我试图在JTable中第一次加载文件名时,第一次按下按钮时,它第一次按loads fine,但第二次按下同一按钮时它goes blank。当按下JButton时,JTable第二次变为空白
短的例子:
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);
}
}
1)为了更好地帮助越早,张贴[MCVE](http://stackoverflow.com/help/mcve)(最小完备可验证实施例)或[SSCCE](HTTP:// WWW。 )(简短,独立,正确的例子)。 2)'btnSearchContent.setBounds(913,175,124,23);'Java GUI必须在不同的操作系统上工作',屏幕大小,屏幕分辨率等。因此,它们不利于像素完美布局。请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[white space]的布局填充和边框(http://stackoverflow.com/a/17874718/ 418556)。 – 2014-12-01 15:36:32
对于'Document Details'下面的东西,使用['JSeparator'](https://docs.oracle.com/javase/8/docs/api/javax/swing/JSeparator.html)。 – 2014-12-01 15:38:42
感谢@AndrewThompson,我试图发布完整的代码,但它给出了格式化错误,这就是为什么只发布了函数,现在已经在pastebin中发布了[完整代码](http://pastebin.com/B4ii3yVQ)谢谢 – 2014-12-01 15:42:09