I am trying to load file name along with few details in the JTable, first time when I press the button, it loads fine, but for the second time when I press the same button it goes blank.
Short example:
package exampleDemo;
public class JTableExample {
private JFrame frame;
private JTable table_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTableExample window = new JTableExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JTableExample() {
initialize();
}
public void showEmptyTable() {
final String[] colNames = {"Sl.no.", "File Name", "Size", "Last Modified", "PO", "LN", "SI", "MS", "PT"};
final String[][] emptyData = {
{"", "", "", "", "", "", "", "", "",},
{"", "", "", "", "", "", "", "", "", },
{"", "", "", "", "", "", "", "", "", },
{"", "", "", "", "", "", "", "", "", },
{"", "", "", "", "", "", "", "", "", },};
table_1.setModel(new DefaultTableModel(emptyData, colNames));
frame.getContentPane().add(table_1);
JScrollPane scrollPane = new JScrollPane(table_1);
scrollPane.setBounds(81, 162, 830, 180);
frame.getContentPane().add(scrollPane);
};
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1002, 576);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
table_1 = new JTable();
table_1.setBounds(81, 162, 830, 180);
frame.getContentPane().add(table_1);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
table_1.repaint();
showEmptyTable();
}
});
btnNewButton.setBounds(578, 70, 89, 23);
frame.getContentPane().add(btnNewButton);
}
}