[英]Refresh all data in JTable
I have seen quite a few threads on refreshing a JTable using: 我已经看到很多使用以下方法刷新JTable的线程:
String[] columnNames = {"column1","column2,......,"columnN"};
String[][] tableData = new String[1][columnNames.length];
JTable table = new JTable(tableData,columnNames);
JScrollPane scrollpane = new JScrollPane(table);
contentPane.add(scrollpane);
... make a change to the data stored in tableData
then call... ...对存储在
tableData
的数据进行更改,然后调用...
AbstractTableModel model = (AbstractTableModel) table.getModel();
model.fireTableDataChanged();
But this doesn't seem to change anything for me. 但这似乎对我没有任何改变。 I need to initially show display a blank table with one row and then update it when the user loads a data file however the code below doesn't seem to work for me.
我首先需要显示显示一行空白表,然后在用户加载数据文件时对其进行更新,但是下面的代码似乎对我不起作用。 I assume its because im redefining
tableData
but this has to be done as i do not know how many rows of data are to be displayed until the user loads a file. 我认为这是因为我正在重新定义
tableData
但是必须这样做,因为在用户加载文件之前,我不知道要显示多少行数据。 How can I update the table? 如何更新表格?
Edit: To make it clearer the code i am running is: 编辑:为了更清楚我正在运行的代码是:
//Initialize Table With 1 Blank Row
String[] columnNames = {"column1","column2,......,"columnN"};
String[][] tableData = new String[1][columnNames.length];
JTable table = new JTable(tableData,columnNames);
JScrollPane scrollpane = new JScrollPane(table);
contentPane.add(scrollpane);
//When the file is loaded resize tableData so it can fit all the file data
tableData = new String[length of data file][columnNames.length];
//Update some of the tableData points
tableData[0][0]="data 1";
tableData[1][0]="data 2";
tableData[0][1]="data 3";
tableData[1][1]="data 4";
....etc
//Tell the JTable to update
AbstractTableModel model = (AbstractTableModel) table.getModel();
model.fireTableDataChanged();
I think the problem is when you create JTable you're not assigning any model to it. 我认为问题在于,当您创建JTable时,您并未为其分配任何模型。
See below example : 参见以下示例:
Its too much in here ,but you'll get the idea. 这里太多了,但是您会明白的。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestGUI {
private JFrame frame;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestGUI window = new TestGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(60, 81, 311, 96);
frame.getContentPane().add(scrollPane);
String[] columnNames = { "column1", "column2", "columnN" };
String[][] tableData = new String[1][columnNames.length];
table = new JTable(tableData, columnNames);
DefaultTableModel model = new DefaultTableModel(2, columnNames.length);
model.addRow(new String[] { "A", "B", "C" });
table.setModel(model);
scrollPane.setViewportView(table);
JButton btnNewButton = new JButton("update");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new String[] { "A", "B", "D" });
}
});
btnNewButton.setBounds(49, 199, 89, 23);
frame.getContentPane().add(btnNewButton);
}
}
Your example will never working because the DefaultTableModel
(which is used here by default) will copy all your array data into internal vector-object. 您的示例将永远无法工作,因为
DefaultTableModel
(默认情况下在此使用)会将所有数组数据复制到内部向量对象中。 So your changes in original array will never be propagated to the internal structure of DefaultTableModel
. 因此,您对原始数组所做的更改将永远不会传播到
DefaultTableModel
的内部结构。
Use DefaultTableModel
directly to avoid your problem. 直接使用
DefaultTableModel
可以避免出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.