【问题标题】:jtable not update after fireTableDataChanged, revalidate and repaintjtable 在 fireTableDataChanged 后不更新,重新验证并重新绘制
【发布时间】:2019-05-12 09:50:28
【问题描述】:

它从 ThingSpeak 下载数据并显示在 jtable 中。我创建了一个“刷新”按钮,它将下载最新数据并显示在现有的 gui 表中。

  1. 获取最新数据...工作
  2. 存储在列表/数组中...工作
  3. 更新 jtable...否

我尝试了 fireTableDataChanged、setModel、revalidate、invalidate 和 repaint,但仍然没有更新表格。我错过了什么?

public class Menu{
protected static List<String> list_name = new ArrayList<>();
// .....(10 more like above)

private JFrame frame = new JFrame("Temp");
private List<String[]> records_data = new ArrayList<String[]>();
private JTable table;
private DefaultTableModel model;
private String[][] data2 = new String[list_channel_ID.size()][11];

String[] columnNames_records = {"Location"};  // skip 10 more items

protected Menu(){
    // Jframe > Jtabbedpane > jtable( I skip all these codes)


    //- Table(Records)
    for(int i = 0; i < list_channel_ID.size(); i++){
        records_data.add(new String[]{ list_name.get(i) });}  // Load data from List to jtable require format, skip 10 items

    //table = new JTable(records_data.toArray(new Object[][] {}), columnNames_records);  // when 'model' is not use

    model = new DefaultTableModel(records_data.toArray(new Object[][] {}), columnNames_records);

    //model = new DefaultTableModel(data2, columnNames);
    table = new JTable(model);


   JMenuItem process_refresh = new JMenuItem("Refresh");

    process_refresh.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {

            // Update the list

            for(int i = 0; i < list_channel_ID.size(); i++){
                records_data.add(new String[]{ list_name.get(i) });   // load from list again, skiped 10 item
            }
            model = new DefaultTableModel(records_data.toArray(new Object[][] {}), columnNames_records);

            model.fireTableDataChanged();
            //table.setModel(model);

            table.revalidate();
            //table.invalidate();
            table.repaint();

        }
    });

}
}

【问题讨论】:

    标签: java swing user-interface jtable repaint


    【解决方案1】:

    问题解决了,我忘记清除列表'records_data' :| 如果有人像我一样面临同样的问题并且心烦意乱 2 天,我会把它留在这里

    工作代码:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    import java.lang.String;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class Menu{
    protected static List<String> list_name = List.of("AAA", "BBB", "CCC");
    // .....(10 more like above)
    
    private JFrame frame = new JFrame("Temp");
    private List<String[]> records_data = new ArrayList<String[]>();
    private List<String[]> result_data = new ArrayList<String[]>();
    private JTable table, table2, table3;
    private DefaultTableModel model;
    private String[][] data2 = new String[3][11];
    
    String[] columnNames_records = {"item A", "item B", "item C"};  // 10 more items
    
    protected Menu(){
        frame.setSize(1000, 600);
        frame.setLayout(new GridLayout(2, 1));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        //- Back Panel
        JPanel panel = new JPanel(null);
        frame.add(panel);
    
        JPanel tab_panel = new JPanel(new GridLayout());
        JTabbedPane tabbedPane = new JTabbedPane();  
        tabbedPane.setBounds(5, 100, 975, 500);
        tabbedPane.add("Records", tab_panel);
        frame.add(tabbedPane);
    
    
        //- Table(Records)
        for(int i = 0; i < 3; i++){
            records_data.add(new String[]{ list_name.get(i) });
        }  // Load data from List to jtable require format, skiped 10 item
    
        //table = new JTable(records_data.toArray(new Object[][] {}), columnNames_records);
        model = new DefaultTableModel(records_data.toArray(new Object[][] {}), columnNames_records);
        //model = new DefaultTableModel(data2, columnNames);
        table = new JTable(model);
        table.setRowHeight(20);
    
    
        //- ScrollPane, allow scrolling if table too long
        JScrollPane scrollPane = new JScrollPane(table);
        tab_panel.add(scrollPane);
    
    
        // Menu bar
        JMenu menu_process = new JMenu("Process");
        JMenuItem process_refresh = new JMenuItem("Refresh");
    
    
        process_refresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
    
                records_data.clear();
                list_name = List.of("DDD", "EEE", "FFF");       // Update the list, hardcode for now
                //list_name.add("KKK");
    
                for(int i = 0; i < 3; i++){
                    records_data.add(new String[]{ list_name.get(i) });   // load from list again, skiped 10 item
                }
                model = new DefaultTableModel(records_data.toArray(new Object[][] {}), columnNames_records);
    
    
                //model.fireTableDataChanged();
                table.setModel(model);
    
                //table.revalidate();
                //table.invalidate();
                //table.repaint();
    
            }
        });
    
        menu_process.add(process_refresh);
    
        JMenuBar menu_bar = new JMenuBar();
        menu_bar.add(menu_process);
        frame.setJMenuBar(menu_bar);
    
        frame.setVisible(true);
    
    }
    
    public static void main(String[ ] args) {
        new Menu();
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 2012-06-18
      相关资源
      最近更新 更多