简体   繁体   English

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

[英]JTable goes blank for second time when JButton is pressed

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 . 我正在尝试在JTable中加载文件名以及一些详细信息,第一次按下按钮时,它可以正常加载 ,但是第二次按下同一按钮时,它变为空白

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

A quick glance at your code would suggest to me that you're going about this the wrong way: you're literally reconstructing a new table model each time you click the button and I suspect (although it's just a hunch at the moment) is because each time you click that button you're adding the same table via a new scrollpanel to the main JFrame - each and every time. 快速浏览一下您的代码将向我建议您使用这种错误的方式:每次单击按钮时,您实际上都是在重建一个新的表模型,但我怀疑(尽管目前只是一种预感)因为每次单击该按钮时,都是通过新的滚动面板将相同的表添加到主JFrame中-每次。

What you want to do is initialise the table and its model during the loading of the frame. 您要做的是在加载框架期间初始化表格及其模型。 And then if you want to clear the table, then simply get the reference to that table's model and manipulate the existing model. 然后,如果要清除表,则只需获取对该表的模型的引用并操作现有模型。 That's it - no need to create an entire new model; 就是这样-无需创建全新模型。 no need to add a new table via a scrollpanel. 无需通过滚动面板添加新表。

So what I'd do in your case is add a line to your constructor: 因此,在您的情况下,我要做的是在构造函数中添加一行:

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

Then initializeYourTable() would look like this: 然后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);
}

To clear the table you can use this shortcut : 要清除表格,您可以使用以下快捷方式

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

And therefore you need to change your actionPerformed : 因此,您需要更改actionPerformed

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM