刷新按钮不刷新 jtable

refresh button doesn't refresh jtable

public class WeatherFrame extends JFrame {

    private JPanel contentPane;
    private JTable table;
    HealthData health = new HealthData();

    private DefaultTableModel model;
    String[] columnNames = {"zipcode", "county", "city", "state", "year", "month","ageGroup",
                            "numOfVisits", "MonthlyMax", "MonthlyMin", "MonthlyNor"};

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WeatherFrame frame = new WeatherFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public WeatherFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 300);
        contentPane = new JPanel();
        contentPane.setBounds(100, 100,750, 200);
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(6, 25, 788, 180);
        contentPane.add(scrollPane);

        populateTable();
        table = new JTable(model);

        scrollPane.setViewportView(table);

        JButton btnInsert = new JButton("insert");
        btnInsert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                InsertFrame frame = new InsertFrame();
                frame.setVisible(true);
            }
        });
        btnInsert.setBounds(279, 217, 117, 29);
        contentPane.add(btnInsert);

        JButton btnDelete = new JButton("delete");
        btnDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DeleteFrame delete = new DeleteFrame();
                delete.setVisible(true);
            }
        });
        btnDelete.setBounds(412, 217, 117, 29);
        contentPane.add(btnDelete);

        JButton btnSearch = new JButton("search");
        btnSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SelectFrame search = new SelectFrame();
                search.setVisible(true);
            }
        });
        btnSearch.setBounds(530, 217, 117, 29);
        contentPane.add(btnSearch);

        JLabel lblWeatherTable = new JLabel("Weather Table");
        lblWeatherTable.setBounds(149, 6, 107, 16);
        contentPane.add(lblWeatherTable);

        JButton btnNext = new JButton("update");
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UpdateFrame update = new UpdateFrame();
                update.setVisible(true);
            }
        });
        btnNext.setBounds(150, 217, 117, 29);
        contentPane.add(btnNext);

        JButton btnRefresh = new JButton("refresh");
        btnRefresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                populateTable();
            }
        });
        btnRefresh.setBounds(29, 217, 117, 29);
        contentPane.add(btnRefresh);

        JButton btnAnalyze = new JButton("Analyze");
        btnAnalyze.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ShowAnalyze analyze = new ShowAnalyze(health.analyze());
                analyze.setVisible(true);
            }
        });
        btnAnalyze.setBounds(662, 217, 117, 29);
        contentPane.add(btnAnalyze);
    }

    @SuppressWarnings("serial")
    public void populateTable() {

        model = new DefaultTableModel(){

            @Override
            public boolean isCellEditable(int row, int column) {
               //all cells false
               return false;
            }
        };

        for(String name: columnNames)
            model.addColumn(name);
        ArrayList<Health> temp = new ArrayList<Health>();
        temp = health.showAllData();
        for(int i = 0; i< temp.size(); i++) {
            Object[] data = {temp.get(i).getZipCode(), temp.get(i).getCounty(), temp.get(i).getCounty(), temp.get(i).getState(),temp.get(i).getYear(),
                             temp.get(i).getMonth(), temp.get(i).getAgeGroup(), temp.get(i).getNumOfVisits(), temp.get(i).getMMax(), temp.get(i).getMMin(), temp.get(i).getMNor()};
            model.addRow(data);
        }
        table.setModel(model);
    }
}

我正在尝试使用刷新按钮刷新 jtable,当我单击该按钮时它似乎正在加载,但之后 table 上没有任何变化。我该如何解决这个问题?在刷新按钮的动作执行方法中,我调用了 populateTable,它是一个将数据加载到 table 的函数。

  • JTable 及其 DefaultTableModel(声明为 private JTable table;private DefaultTableModel model;)不知道(重新)创建了新的 model = new DefaultTableModel(){public void populateTable() {

你必须

  • JTables 实例添加一个新的 DefaultTableModel,该实例已在您的 Swing GUI 中可见

  • (更好的选择是)直接向private DefaultTableModel model;添加新数据,此模型指定用于

  • 休息在@Andrew Thompson

  • 的评论中有很好的描述

populateTable 方法中,您更改了 table 模型,但没有将该新模型传递给 JTable

选项 1:替换 table 模型

在构造函数中,您调用:

table = new JTable(model);
populateTable();

populateTable 中,我希望是这样的:

table.setModel(model);

选项 2:更新 table 模型

正如 mKorbel 已经建议的那样,您还可以更新 table 模型,而不是丢弃现有模型并创建一个新模型。您的 populateTable 方法可能如下所示(使用 Java 8 并使用新的 initializeModel 方法最初创建 table 模型):

public void populateTable() {
    boolean firstTime = (model == null);

    if (firstTime) {
        initializeModel();
    } else {
        model.getDataVector().clear();
    }

    for (Health item : health.showAllData()) {
        model.addRow(new Vector<>(Arrays.asList(
                item.getZipCode(), item.getCounty(), item.getState(), item.getYear(),
                item.getMonth(), item.getAgeGroup(), item.getNumOfVisits(),
                item.getMMax(), item.getMMin(), item.getMNor()
        )));
    }

    if (firstTime && table != null) {
        table.setModel(model);
    }
}

private void initializeModel() {
    model = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int row, int column) {
            //all cells false
            return false;
        }
    };

    for (String name : columnNames)
        model.addColumn(name);
}