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.
Another Java Source Code:
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
}
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 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();
}
}
Download Projects Source Code