刷新按钮不刷新jtable

时间:2015-12-03 12:30:41

标签: java swing jframe jtable jbutton

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

2 个答案:

答案 0 :(得分:3)

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

你必须

  • 将新的DefaultTableModel添加到已在Swing GUI中显示的JTable实例

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

  • 在@ Andrew Thompson的评论中很好地描述了休息

答案 1 :(得分:3)

populateTable方法中,您更改了表格模型,但未将新模型传递给JTable

选项1:替换表格型号

在构造函数中,您调用:

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

populateTable中,我希望有类似的内容:

table.setModel(model);

选项2:更新表格模型

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

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