我有一个 JTable,其单元格是可编辑的。但是,如果我编辑单元格并刷新表格。更改不会保存。这就是我定义表格的方式:
String [] columnNames = {"Application number",
"Name",
"date",
"fileLoc",
"Country"};
//Data for the table
String [][] data = table.tableArray;//tableArray is an array of array of strings.
TableModel model = new DefaultTableModel(data, columnNames);
JTable mainTable = new JTable(model);
model.isCellEditable(data.length,columnNames.length);
contentPane.add(new JScrollPane (mainTable));
我在网上查看过,但似乎找不到任何关于保存对单元格所做的更改的建议。非常欢迎任何帮助!
最佳答案
I guess i'm not refreshing the table as such. I use frame.dispose() and then create a new frame with the table in.
那么您就丢失了在当前 DefaultTableModel 中所做的所有更改
不要使用新的 DefaultTableModel 和新的 JTable 创建新的 JFrame
TableCellEditor 中的所有更改都会在 JTables View 中显示
JTable(及其模型)已为此作业做好准备,无需在运行时重新创建这些对象
DefaultTableModel 已实现所有通知程序,无需重写任何其他事件,也无需以编程方式 fireXxxXxx(),但 AbstractTableModel 需要这些事件
define 为您的 JTable 添加此代码行
mainTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
DefaultTableModel 代表您所需的二维数组
关于java - 将 JTable 编辑保存到二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15414555/