C# - How To Update A DataGridView Row With TextBox In C#

C# - How To Update A DataGridView Row With TextBoxes In C#

__________________________________________________________________________

In This C# Code We Will See How To  Update A DataGridView Row Using TextBoxes In CSharp Programming Language .



Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Update_DataGridView_Using_TextBoxes : Form
    {
        public Update_DataGridView_Using_TextBoxes()
        {
            InitializeComponent();
        }

        DataTable table = new DataTable();
        int indexRow;

        private void Update_DataGridView_Using_TextBoxes_Load(object sender, EventArgs e)
        {
   // set datatable columns values
   table.Columns.Add("Id", typeof(int));// data type int
   table.Columns.Add("First Name", typeof(string));// datatype string
   table.Columns.Add("Last Name", typeof(string));// datatype string
   table.Columns.Add("Age", typeof(int));// data type int

            table.Rows.Add(1, "First A", "Last A", 10);
            table.Rows.Add(2, "First B", "Last B", 20);
            table.Rows.Add(3, "First C", "Last C", 30);
            table.Rows.Add(4, "First D", "Last D", 40);
            table.Rows.Add(5, "First E", "Last E", 50);
            table.Rows.Add(6, "First F", "Last F", 60);
            table.Rows.Add(7, "First G", "Last G", 70);
            table.Rows.Add(8, "First H", "Last H", 80);

            dataGridView1.DataSource = table;

        }

        // Get Selected Row Values From DataGridView Into TextBox
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            indexRow = e.RowIndex; // get the selected Row Index
            DataGridViewRow row = dataGridView1.Rows[indexRow];

            textBoxID.Text = row.Cells[0].Value.ToString();
            textBoxFN.Text = row.Cells[1].Value.ToString();
            textBoxLN.Text = row.Cells[2].Value.ToString();
            textBoxAGE.Text = row.Cells[3].Value.ToString();
        }

        //update datagridview row data 
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataGridViewRow newDataRow = dataGridView1.Rows[indexRow];
            newDataRow.Cells[0].Value = textBoxID.Text;
            newDataRow.Cells[1].Value = textBoxFN.Text;
            newDataRow.Cells[2].Value = textBoxLN.Text;
            newDataRow.Cells[3].Value = textBoxAGE.Text;
        }
    }
}

 
/////////////////////////// OUTPUT: 
C# Update DataGridView Selected Row Using TextBoxes
DatagridView Row Uodated



ALSO SEE : 

Add A Row To DataGridView From TextBox
Remove A Row From DataGridView
Get Selected Row Values From DataGridView Into TextBox
Add Delete And Update DataGridView Row Using TextBoxes

C# - How To Get Selected Row Values From DataGridView Into TextBox In C#

C# - How To Displaying Data From Selected Rows In DataGridView into TextBox Using C#

                                                                                                                                                     

In This C# Code We Will See How To Get A DataGridView Row Values Into  TextBoxes 
In C# Programming Language.




Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class DataGridView_Row_Data_To_TextBoxes : Form
    {
        public DataGridView_Row_Data_To_TextBoxes()
        {
            InitializeComponent();
        }

        DataTable table = new DataTable();

        private void DataGridView_Row_Data_To_TextBoxes_Load(object sender, EventArgs e)
        {
            // set datatable columns values
            table.Columns.Add("Id", typeof(int));// data type int
            table.Columns.Add("First Name", typeof(string));// data type string
            table.Columns.Add("Last Name", typeof(string));// data type int
            table.Columns.Add("Age", typeof(int));// data type string

            table.Rows.Add(1, "First A", "Last A", 10);
            table.Rows.Add(2, "First B", "Last B", 20);
            table.Rows.Add(3, "First C", "Last C", 30);
            table.Rows.Add(4, "First D", "Last D", 40);
            table.Rows.Add(5, "First E", "Last E", 50);
            table.Rows.Add(6, "First F", "Last F", 60);
            table.Rows.Add(7, "First G", "Last G", 70);
            table.Rows.Add(8, "First H", "Last H", 80);

            dataGridView1.DataSource = table;

        }

        //Get Selected Row Values From DataGridView Into TextBox
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;// get the Row Index
            DataGridViewRow selectedRow = dataGridView1.Rows[index];
            textBoxID.Text = selectedRow.Cells[0].Value.ToString();
            textBoxFN.Text = selectedRow.Cells[1].Value.ToString();
            textBoxLN.Text = selectedRow.Cells[2].Value.ToString();
            textBoxAGE.Text = selectedRow.Cells[3].Value.ToString();

        }
    }
}


C# - How To Remove A Row From DataGridView In C#

C# - How To Delete The Selected Row From DataGridView Using C#


                                                                                                                                                     

In This C# Code We Will See How To Remove The Selected Row From DataGridView 
In C# Programming Language.




Source Code:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Remove_DataGridView_Selected_Row : Form
    {
        public Remove_DataGridView_Selected_Row()
        {
            InitializeComponent();
        }

        DataTable table = new DataTable();

        private void Remove_DataGridView_Selected_Row_Load(object sender, EventArgs e)
        {
     // set datatable columns values
     table.Columns.Add("Id", typeof(int));// data type int
     table.Columns.Add("First Name", typeof(string));// datatype string
     table.Columns.Add("Last Name", typeof(string));// datatype string
     table.Columns.Add("Age", typeof(int));// data type int

            table.Rows.Add(1, "First A", "Last A", 10);
            table.Rows.Add(2, "First B", "Last B", 20);
            table.Rows.Add(3, "First C", "Last C", 30);
            table.Rows.Add(4, "First D", "Last D", 40);
            table.Rows.Add(5, "First E", "Last E", 50);
            table.Rows.Add(6, "First F", "Last F", 60);
            table.Rows.Add(7, "First G", "Last G", 70);
            table.Rows.Add(8, "First H", "Last H", 80);

            dataGridView1.DataSource = table;
        }

        // Remove Selected Row From DataGridView
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //get the index of the selected row in datagridview
            //and delete a row from datatable
            //and bind the datagridview with datatable
            int rowIndex = dataGridView1.CurrentCell.RowIndex;
            dataGridView1.Rows.RemoveAt(rowIndex);

        }
    }
}

//////////////////////// OUTPUT:

C# Remove DataGridView Selected Row
Before Removing

C# Remove DataGridView Selected Row
After Removing


ALSO SEE : 

Add A Row To DataGridView From TextBox
Get Selected Row Values From DataGridView Into TextBox
Update A DataGridView Row With TextBox
Add Delete And Update DataGridView Row Using TextBoxes

C# - How To Add A Row To DataGridView From TextBox In C#

C# - How To Insert Data From Textboxes To DataGridView In C#

                                                                                                                                                     

In This C# Code We Will See How To Add A Row To DataGridView From TextBoxes 
In C# Programming Language.



Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Add_Row_To_DataGridView_Using_TextBoxes : Form
    {
        public Add_Row_To_DataGridView_Using_TextBoxes()
        {
            InitializeComponent();
        }
        DataTable table = new DataTable();

        private void Add_Row_To_DataGridView_Using_TextBoxes_Load(object sender, EventArgs e)
        {
   // set datatable columns values
   table.Columns.Add("Id", typeof(int));// data type int
   table.Columns.Add("First Name", typeof(string));// datatype string
   table.Columns.Add("Last Name", typeof(string));// datatype string
   table.Columns.Add("Age", typeof(int));// data type int

            dataGridView1.DataSource = table;
        }

         //add a row to datatable
        private void btnAdd_Click(object sender, EventArgs e)
        {
           
            table.Rows.Add(textBoxID.Text, textBoxFN.Text, textBoxLN.Text, textBoxAGE.Text);
            dataGridView1.DataSource = table;

        }
    }
}



/////////////////////////////////OUTPUT:
C# Add Row To DataGridView From TextBoxes
Row Added



ALSO SEE : 

Remove A Row From DataGridView
Get Selected Row Values From DataGridView Into TextBox
Update A DataGridView Row With TextBox
Add Delete And Update DataGridView Row Using TextBoxes

JAVA - How To Update A Row From JTable Using JTextfield In Java

JAVA - How To Update JTable Selected Row Using JTextfield In Java

__________________________________________________________________________

In This Java Code We Will See How To Update A Selected Row From JTable  Using 
JTextfield In Java Programming Language With NetBeans.






Source Code:


package java_tutorials;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 * @author 1bestcsharp.blogspot.com
 */
public class Java_JTable_Update_Selected_Row_Using_TextBoxes extends javax.swing.JFrame {

    /**
     * Creates new form Java_JTable_Update_Selected_Row_Using_TextBoxes
     */
    public Java_JTable_Update_Selected_Row_Using_TextBoxes() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldID = new javax.swing.JTextField();
        jTextFieldFN = new javax.swing.JTextField();
        jTextFieldLN = new javax.swing.JTextField();
        jTextFieldAGE = new javax.swing.JTextField();
        btnUpdateRow = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel1.setText("Id :");

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel2.setText("First Name :");

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel3.setText("Last Name :");

        jLabel4.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel4.setText("Age :");

        btnUpdateRow.setText("Update Row");
        btnUpdateRow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateRowActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "DDFFD", "DGHYTGD", "344"},
                {"2", "FGHTYH", "UYUYHF", "33"},
                {"3", "JHGF", "JHGFD", "254"},
                {"4", "WQER", "OUYREE", "38"},
                {"5", "BSGH", "IYDGH", "22"},
                {"6", "QSDTYKJH", "PIHGDX", "16"},
                {"7", "DSZDD", "UTRFRT", "2567"},
                {"8", "XCVBGR", "YTEUI", "56"},
                {"9", "FGHJUY", "NGFDBNJ", "65"},
                {"10", "WQAZR", "VXWG", "42"}
            },
            new String [] {
                "Id", "First Name", "Last Name", "Age"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTextFieldFN, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldAGE, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldLN, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(27, 27, 27))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnUpdateRow, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldFN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldLN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldAGE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addComponent(btnUpdateRow, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                      

    private void btnUpdateRowActionPerformed(java.awt.event.ActionEvent evt) {                                          

        int i = jTable1.getSelectedRow();
         DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
        if(i >= 0)
        {
            model.setValueAt(jTextFieldID.getText(), i, 0);
            model.setValueAt(jTextFieldFN.getText(), i, 1);
            model.setValueAt(jTextFieldLN.getText(), i, 2);
            model.setValueAt(jTextFieldAGE.getText(), i, 3);
        }else{
            JOptionPane.showMessageDialog(null, "Error");
        }
     
    }                                          

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                  
   
        int selectedRow = jTable1.getSelectedRow();
        DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
        jTextFieldID.setText(model.getValueAt(selectedRow, 0).toString());
        jTextFieldFN.setText(model.getValueAt(selectedRow, 1).toString());
        jTextFieldLN.setText(model.getValueAt(selectedRow, 2).toString());
        jTextFieldAGE.setText(model.getValueAt(selectedRow, 3).toString());
    }                                  

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Update_Selected_Row_Using_TextBoxes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Update_Selected_Row_Using_TextBoxes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Update_Selected_Row_Using_TextBoxes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Update_Selected_Row_Using_TextBoxes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Java_JTable_Update_Selected_Row_Using_TextBoxes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                  
    private javax.swing.JButton btnUpdateRow;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldAGE;
    private javax.swing.JTextField jTextFieldFN;
    private javax.swing.JTextField jTextFieldID;
    private javax.swing.JTextField jTextFieldLN;
    // End of variables declaration                
}

/////////////////////////////// OUTPUT:


update jtable selected row using jtextfields in java
JTable Selected Row Updated From JTextFields




Another Source Code:

package JavaDB_001;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;


public class Work extends JFrame{

    JTextField JT_fname,JT_lname,JT_age,JT_id;

    JButton btn_add,btn_update;

    JTable table = new JTable();

    JScrollPane pane;

   Object[] cols = null;

 public Work(){   

     JT_id = new JTextField(20);
     JT_fname = new JTextField(20);
     JT_lname = new JTextField(20);
     JT_age = new JTextField(20);

     JT_id.setBounds(130,20,150,20);
     JT_fname.setBounds(130, 50, 150, 20);
     JT_lname.setBounds(130, 80, 150, 20);
     JT_age.setBounds(130, 110, 150, 20);

     btn_add = new JButton("ADD");
     btn_add.setBounds(300, 30, 100, 20);

     btn_update = new JButton("UPDATE");
     btn_update.setBounds(300,100,100,20);

       cols = new String[]{"id","fname","lname","age"};
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            model.setColumnIdentifiers(cols);

       btn_add.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {

              model.addRow(new Object[]{
                               JT_id.getText(),JT_fname.getText(),
                               JT_lname.getText(),JT_age.getText()
                                       });
                  }
             });
       
       
       // Get Selected Row Values From JTable Into JTextfields
          table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ 
          @Override
          public void valueChanged(ListSelectionEvent e) {
          
                int i = table.getSelectedRow();
                JT_id.setText((String)model.getValueAt(i, 0));
                JT_fname.setText((String)model.getValueAt(i, 1));
                JT_lname.setText((String)model.getValueAt(i, 2));
                JT_age.setText((String)model.getValueAt(i, 3));
            }
        });
       
// Update A  JTable Row Using JTextfield
      btn_update.addActionListener(new ActionListener(){
      
      @Override
      public void actionPerformed(ActionEvent e){
          
          int i = table.getSelectedRow();
          model.setValueAt(JT_id.getText(), i, 0);
          model.setValueAt(JT_fname.getText(), i, 1);
          model.setValueAt(JT_lname.getText(), i, 2);
          model.setValueAt(JT_age.getText(), i, 3);
      }
      });    
       
     pane = new JScrollPane(table);
     pane.setBounds(100,150,300,300);
     setLayout(null);
  
     add(pane);
     add(JT_id);
     add(JT_fname);
     add(JT_lname);
     add(JT_age);
     add(btn_add);
     add(btn_update);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
     Color c = Color.decode("#bdb76b");
     getContentPane().setBackground(c);
     setLocationRelativeTo(null);
     setSize(500,500);
    
 }
   public static void main(String[] args){
       new  Work();
   }
}


JAVA - How To Get Selected Row Values From JTable Into JTextfields In Java

JAVA - How To Get Value from JTable And Set It To JTextfield In Java

________________________________________________________________________

In This Java Code We Will See How To Display Value From A JTable Selected Row To JTextfields In Java Programming Language.






Java Source Code:

package java_tutorials;

import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 1bestcsharp.blogspot.com
 */
public class Java_JTable_Get_Selected_Row_To_TextFields extends javax.swing.JFrame {

    /**
     * Creates new form Java_JTable_Get_Selected_Row_To_TextFields
     */
    public Java_JTable_Get_Selected_Row_To_TextFields() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jTextFieldID = new javax.swing.JTextField();
        jTextFieldFN = new javax.swing.JTextField();
        jTextFieldLN = new javax.swing.JTextField();
        jTextFieldAGE = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel4.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel4.setText("Age :");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "HGFD", "JHGFD", "3456"},
                {"2", "JHHGF", "JHGFD", "987654"},
                {"3", "JHGFDBFDS", "IUYTREDFGHJ", "2345678"},
                {"4", "AQWXFRTYUI", "IUYTREDFGHJK", "54343"},
                {"5", "POIUYTR", "MLKJHGF", "2345"},
                {"6", "ADFHU", "YREZFN?L", "234567"},
                {"7", "BGFERYUIK", "NUIOPKI", "597"},
                {"8", "QDFJOIU", "NVCSGYO", "23457"},
                {"9", "AZTUO", "IHGBDDH", "633"},
                {"10", "EDFHJ", "VXNJH", "2457"}
            },
            new String [] {
                "Id", "First Name", "Last Name", "Age"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel1.setText("Id :");

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel2.setText("First Name :");

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel3.setText("Last Name :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jTextFieldFN, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldAGE, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldLN, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldFN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldLN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldAGE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                      

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        
        // get the model from the jtable
       DefaultTableModel model = (DefaultTableModel)jTable1.getModel();

        // get the selected row index
       int selectedRowIndex = jTable1.getSelectedRow();
       
        // set the selected row data into jtextfields
       jTextFieldID.setText(model.getValueAt(selectedRowIndex, 0).toString());
       jTextFieldFN.setText(model.getValueAt(selectedRowIndex, 1).toString());
       jTextFieldLN.setText(model.getValueAt(selectedRowIndex, 2).toString());
       jTextFieldAGE.setText(model.getValueAt(selectedRowIndex, 3).toString());
        
    }                                  

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Get_Selected_Row_To_TextFields.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Get_Selected_Row_To_TextFields.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Get_Selected_Row_To_TextFields.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Get_Selected_Row_To_TextFields.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Java_JTable_Get_Selected_Row_To_TextFields().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                  
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldAGE;
    private javax.swing.JTextField jTextFieldFN;
    private javax.swing.JTextField jTextFieldID;
    private javax.swing.JTextField jTextFieldLN;
    // End of variables declaration                
}


///////////////////////////////////////// OUTPUT:


display jtable selected row values into jtextfields in java
Display Selected Row In JTextFields





Another Java Source Code:

package JavaDB_001;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;


public class Work extends JFrame{
 JTextField JT_fname,JT_lname,JT_age,JT_id;
JButton btn_add;
 JTable table = new JTable();
 JScrollPane pane;
 Object[] cols = null;
 public Work(){   
     JT_id = new JTextField(20);
     JT_fname = new JTextField(20);
     JT_lname = new JTextField(20);
     JT_age = new JTextField(20);
     JT_id.setBounds(130,20,150,20);
     JT_fname.setBounds(130, 50, 150, 20);
     JT_lname.setBounds(130, 80, 150, 20);
     JT_age.setBounds(130, 110, 150, 20);
     btn_add = new JButton("ADD");
     btn_add.setBounds(300, 70, 100, 20);
      
       cols = new String[]{"id","fname","lname","age"};
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            model.setColumnIdentifiers(cols);
       btn_add.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {

              model.addRow(new Object[]{
                               JT_id.getText(),JT_fname.getText(),
                               JT_lname.getText(),JT_age.getText()
                                       });
                  }
             });
       
       
         //Get Selected Row Values From JTable Into JTextfields In Java
          table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ 
          @Override
          public void valueChanged(ListSelectionEvent e) {
          
                int i = table.getSelectedRow();
                JT_id.setText((String)model.getValueAt(i, 0));
                JT_fname.setText((String)model.getValueAt(i, 1));
                JT_lname.setText((String)model.getValueAt(i, 2));
                JT_age.setText((String)model.getValueAt(i, 3));
            }
        });
       
       
     pane = new JScrollPane(table);
     pane.setBounds(100,150,300,300);

       
     setLayout(null);
  
     add(pane);
     add(JT_id);
     add(JT_fname);
     add(JT_lname);
     add(JT_age);
     add(btn_add);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
     Color c = Color.decode("#bdb76b");
     getContentPane().setBackground(c);
     setLocationRelativeTo(null);
     setSize(500,500);
    
 }
   public static void main(String[] args){
       new  Work();
   }
}
/////////////////////////////////////////OUTPUT:
How To Get Selected Row Values From JTable Into JTextfields In Java
Show Selected Row In JTextFields