gpt4 book ai didi

java - 当按下 JButton 时,JTable 第二次变为空白

转载 作者:行者123 更新时间:2023-12-01 12:11:45 25 4
gpt4 key购买 nike

我正在尝试加载文件名以及 JTable 中的一些详细信息,当我第一次按下按钮时,它 loads fine ,但是当我第二次按下同一个按钮时,它 goes blank .

简短示例:

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);
}
}

最佳答案

快速浏览一下您的代码,我会发现您正在以错误的方式处理此问题:每次单击按钮时,您实际上都会重建一个新的表模型,我怀疑(尽管这只是预感)时刻)是因为每次单击该按钮时,您都会通过新的滚动面板将同一个表添加到主 JFrame - 每次。

您想要做的是在加载框架期间初始化表格及其模型。然后,如果您想清除该表,只需获取对该表模型的引用并操作现有模型即可。就是这样 - 不需要创建一个全新的模型;无需通过滚动面板添加新表格。

因此,在您的情况下,我要做的就是在构造函数中添加一行:

public YourClassName() {
initialize();
initializeYourTable();
}

然后 initializeYourTable() 看起来像这样:

private void initSearchTable() {
final String[] colNames = {"Sl.no.", "File Name", "Size", "Last Modified", "PO", "LN", "SI", "MS", "PT"};
//PO: Part Order
//LN: Line Number
//SI: Serial Number
//MS: Material Supplier
//PT: Part Item
final String[][] emptyData = {
{"", "", "", "", "", "", "", "", "",},
{"", "", "", "", "", "", "", "", "",},
{"", "", "", "", "", "", "", "", "",},
{"", "", "", "", "", "", "", "", "",},
{"", "", "", "", "", "", "", "", "",},};
table_3.setModel(new DefaultTableModel(emptyData, colNames));
//frmViperManufacturingRecord.getContentPane().add(table_3); <-- NB this should be commented out!!
table_3.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table_3.getColumnModel().getColumn(0).setPreferredWidth(40);
table_3.getColumnModel().getColumn(1).setPreferredWidth(290);
table_3.getColumnModel().getColumn(2).setPreferredWidth(80);
table_3.getColumnModel().getColumn(3).setPreferredWidth(178);
table_3.getColumnModel().getColumn(4).setPreferredWidth(25);
table_3.getColumnModel().getColumn(5).setPreferredWidth(25);
table_3.getColumnModel().getColumn(6).setPreferredWidth(25);
table_3.getColumnModel().getColumn(7).setPreferredWidth(25);
table_3.getColumnModel().getColumn(8).setPreferredWidth(25);
JScrollPane scrollPane = new JScrollPane(table_3);
scrollPane.setBounds(324, 209, 713, 160);
frmViperManufacturingRecord.getContentPane().add(scrollPane);
}

要清除表格,您可以使用此 shortcut :

private void resetTable() {
DefaultTableModel model = (DefaultTableModel)table_3.getModel();
model.setRowCount(0);
}

因此您需要更改您的actionPerformed:

public void actionPerformed(ActionEvent arg0) {
table_3.repaint();
//showEmptyTable();
resetTable();
}

关于java - 当按下 JButton 时,JTable 第二次变为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27231749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com