Java - How To Display JTable Selected Rows To Another JFrame JTabel In Java
In this Java Tutorial we will see How To Get Jtable Selected Rows And Display Them In A Second Jtable In Another JFrame In Java NetBeans .
Project Source Code:
package javaapp;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/**
*
* @author 1BestCsharp
*/
public class JFrame1 extends javax.swing.JFrame {
/**
* Creates new form JFrame1
*/
public JFrame1() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
Export_Rows = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"F1", "L1", "10", "CN1"},
{"F2", "L2", "15", "CN2"},
{"F3", "L3", "20", "CN3"},
{"F4", "L4", "25", "CN4"},
{"F5", "L5", "30", "CN5"},
{"F6", "L6", "35", "CN6"},
{"F7", "L7", "40", "CN7"},
{"F8", "L8", "45", "CN8"},
{"F9", "L9", "50", "CN9"},
{"F10", "L10", "55", "CN10"},
{"F11", "L11", "60", "CN11"},
{"F12", "L12", "65", "CN12"}
},
new String [] {
"First Name", "Last Name", "Age", "Country"
}
));
jScrollPane1.setViewportView(jTable1);
Export_Rows.setText("Show Selected Row In Another JFrame");
Export_Rows.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Export_RowsActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(79, 79, 79)
.addComponent(Export_Rows)))
.addContainerGap(51, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Export_Rows)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(37, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void Export_RowsActionPerformed(java.awt.event.ActionEvent evt) {
TableModel model1 = jTable1.getModel();
int indexs[] = jTable1.getSelectedRows();
Object[] row = new Object[4];
JFrame2 frm2 = new JFrame2();
DefaultTableModel model2 = (DefaultTableModel)frm2.jTable1.getModel();
for(int i = 0; i < indexs.length; i++)
{
row[0] = model1.getValueAt(indexs[i], 0);
row[1] = model1.getValueAt(indexs[i], 1);
row[2] = model1.getValueAt(indexs[i], 2);
row[3] = model1.getValueAt(indexs[i], 3);
model2.addRow(row);
}
frm2.setVisible(true);
}
/**
* @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(JFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrame1.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 JFrame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Export_Rows;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
////// OUTPUT :