爲什麼不將表格單元格0,1從aaa更改爲XXXX?JTable將不會刷新
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class MainFrame {
public static void main(String[] args) {
JFrame f = new JFrame("Refreshing JTable");
JPanel p = new JPanel(new GridBagLayout());
DefaultTableModel productsModel;
JTable productsTable;
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] tableTitle = new String[] {"ID", "Name"};
String[][] tableData = {{"1", "AAA"},{"2", "BBB"}};
productsModel = new DefaultTableModel(tableData, tableTitle);
productsTable = new JTable(productsModel) {
public boolean isCellEditable(int r, int c) {
return false;
}
};
JScrollPane scrollpane = new JScrollPane(productsTable);
tableData[0][1] = "XXXX";
f.add(p);
p.add(scrollpane);
f.validate();
f.setVisible(true);
}
}
原因: 顯然試圖更新數據存儲位置將導致JTable中不改變陣列。無論是DefaultTableModel需要更新或整個表需要重繪。
編輯(可能的解決方案)的一種方法是使用定時器:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class MainFrame {
static JFrame f = new JFrame("Refreshing JTable");
static JPanel p = new JPanel(new GridBagLayout());
static DefaultTableModel productsModel;
static JTable productsTable;
public static void main(String[] args) {
runGui();
}
@SuppressWarnings("serial")
public static void runGui() {
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] tableTitle = new String[] {"ID", "Name"};
String[][] tableData = {{"1", "AAA"},{"2", "BBB"}};
productsModel = new DefaultTableModel(tableData, tableTitle);
productsTable = new JTable(productsModel) {
public boolean isCellEditable(int r, int c) {
return false;
}
};
JScrollPane scrollpane = new JScrollPane(productsTable);
tableData[0][1] = "XXXX";
Timer t = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addColumns();
remakeData();
productsTable.setModel(productsModel);
}
});
t.start();
f.add(p);
p.add(scrollpane);
f.validate();
f.setVisible(true);
}
private static void addColumns() {
productsModel.setColumnCount(0);
productsModel.addColumn("ID");
productsModel.addColumn("Name");
}
private static void remakeData() {
productsModel.setRowCount(0);
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"1", "Dummy item 1"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"2", "Dummy itme 2"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"3", "Dummy item 3"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"4", "Dummy item 4"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"5", "Dummy item 5"});
}
}
EDIT(更好的解決方案,它完美地爲我工作的方式)使用靜態方法。由於我通過另一個Frame在數組中添加新數據,因此我在MainFrame中創建了一個靜態方法,每當我在數組中添加/更新/刪除Object時,都會調用它。此方法將在更新後重新創建整個模型,並因此將刷新表格。
爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-03-20 18:59:53
我第二次是Andrew的偉大建議,因爲我認爲根據目前的信息我們無法猜測出現了什麼問題。還可以考慮使用豐盛的System.out.println調用來爲代碼添加代碼,以幫助調試錯誤源並確保您的假設都是正確的。 – 2012-03-20 19:12:28
可能重複的[Java,如何刷新JTable](http://stackoverflow.com/questions/9790883/java-how-to-refres-jtable) – kleopatra 2012-03-20 19:14:00