gpt4 book ai didi

java - fireTableDataChanged、重新验证和重绘后 jtable 不更新

转载 作者:行者123 更新时间:2023-12-02 05:45:42 26 4
gpt4 key购买 nike

它从 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();

}
});

}
}

最佳答案

问题已解决,我忘记清除列表“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();
}

}

关于java - fireTableDataChanged、重新验证和重绘后 jtable 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56098025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com