In this Java Tutorial we will see How To Populate And Delete JTabe Content In Java Using NetBeans .
Subscribe : https://www.youtube.com/channel/UCS3W5vFugqi6QcsoAIHcMpw
Project Source Code:
import javax.swing.table.DefaultTableModel;
/**
*
* @author 1BestCsharp
*/
public class JTable_Load_Clear_Data extends javax.swing.JFrame {
/**
* Creates new form JTable_Load_Clear_Data
*/
public JTable_Load_Clear_Data() {
initComponents();
ShowData();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButtonLoad = new javax.swing.JButton();
jButtonClear = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane1.setViewportView(jTable1);
jButtonLoad.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButtonLoad.setText("Load");
jButtonLoad.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonLoadActionPerformed(evt);
}
});
jButtonClear.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButtonClear.setText("Clear");
jButtonClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonClearActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButtonLoad, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonClear, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
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, 312, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addComponent(jButtonLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButtonClear, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(14, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public void ShowData()
{
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
String[] cols = {"Id","First Name","Last Name","Age"};
String[][] data = {
{"1", "FN1","LN1","10"},
{"2", "FN1","LN1","15"},
{"3", "FN1","LN1","20"},
{"4", "FN1","LN1","25"},
{"5", "FN1","LN1","30"},
{"6", "FN1","LN1","35"},
{"7", "FN1","LN1","40"},
{"8", "FN1","LN1","45"},
{"9", "FN1","LN1","50"},
{"10", "FN1","LN1","60"}
};
model.setDataVector(data, cols);
}
private void jButtonLoadActionPerformed(java.awt.event.ActionEvent evt) {
ShowData();
}
private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {
jTable1.setModel(new DefaultTableModel(null,new String[]{"Id","First Name","Last Name","Age"}));
}
/**
* @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(JTable_Load_Clear_Data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JTable_Load_Clear_Data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JTable_Load_Clear_Data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JTable_Load_Clear_Data.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 JTable_Load_Clear_Data().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonClear;
private javax.swing.JButton jButtonLoad;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
#java