我試圖在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