JXTable not refreshed upon button click

旧巷老猫 提交于 2019-12-02 10:39:33

There are some conceptual mistakes in the lines below:

String[] columnNames = {"SHEET_NAME", "VALIDATION_NAME", "#"};
DefaultTableModel tmodel = new DefaultTableModel(0, 0);
tmodel.setColumnIdentifiers(columnNames);
JXTable roTable = new JXTable();
...
JScrollPane scrPane = new JScrollPane(roTable);
...
overviewPanel.add(scrPane);

1) Don't create a new JXTable when you press the button but work with the table model instead either by clearing the current table model and adding rows to it or directly by setting a new one. For example:

String[] columnNames = {"SHEET_NAME", "VALIDATION_NAME", "#"};
DefaultTableModel tmodel = new DefaultTableModel(0, 0);
tmodel.setColumnIdentifiers(columnNames);
yourTable.setModel(tmodel);

2) These lines suggests that overviewPanel has already been displayed by the time you are trying to add the new table by clicking the button, thus invalidating the components hierarchy and in consequence you have to revalidate and repaint the panel like this:

overviewPanel.add(scrPane);
overviewPanel.revalidate();
overviewPanel.repaint();

However while we can add components dynamically in Swing we tipically place all our components before the top-level container (window) is made visible. Thus the approach described in point 1 is highly preferable over this one and I'm adding this point just for completeness.

3) Be aware that time consuming tasks such as database calls or IO operations may block the Event Dispatch Thread (EDT) causing the GUI become unresponsive. The EDT is a single and special thread where Swing components creation and update take place. To avoid block this thread consider use a SwingWorker to perform heavy tasks in a background thread and update Swing components in the EDT. See more in Concurrency in Swing lesson.


Update

Please consider the following example illustrating point 1:

  • The table is created and placed once before making the top-level container (window) visible.
  • Both actions work with the table model: one of them sets a new table model and the other one clear and re-fill the current table model.

Here is the code. Hope it helps!

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.JXTable;

public class Demo {

    private void createAndShowGUI() {

        final JXTable table = new JXTable(5, 6);
        table.setPreferredScrollableViewportSize(new Dimension(500, 200));

        Action resetModelAction = new AbstractAction("Set a new model") {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random random = new Random(System.currentTimeMillis());
                DefaultTableModel model = new DefaultTableModel(0, 6);

                for (int i = 0; i < model.getColumnCount(); i++) {
                    model.addRow(new Object[]{
                        random.nextInt(),
                        random.nextInt(),
                        random.nextInt(),
                        random.nextInt(),
                        random.nextInt(),
                        random.nextInt()
                    });
                }

                table.setModel(model);
            }
        };

        Action clearAndFillModelAction = new AbstractAction("Clear and fill model") {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random random = new Random(System.currentTimeMillis());
                DefaultTableModel model = (DefaultTableModel)table.getModel();
                model.setRowCount(0); // clear the model

                for (int i = 0; i < model.getColumnCount(); i++) {
                    model.addRow(new Object[]{
                        random.nextInt(),
                        random.nextInt(),
                        random.nextInt(),
                        random.nextInt(),
                        random.nextInt(),
                        random.nextInt()
                    });
                }
            }
        };

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(new JButton(resetModelAction));
        buttonsPanel.add(new JButton(clearAndFillModelAction));

        JPanel content = new JPanel(new BorderLayout(8,8));
        content.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
        content.add(new JScrollPane(table));
        content.add(buttonsPanel, BorderLayout.PAGE_END);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(content);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().createAndShowGUI();
            }
        });
    }    
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!