Java-如何更改单元格时保存JTable? [英] Java-How to save JTable when cells are changed?
问题描述
我的JPanel中有一个JTable,我想知道如何保存编辑后的单元格.现在发生的是,当我编辑单元格并按ENTER键时,将显示新数据.但是当我关闭程序并再次打开它时,数据将恢复为原来的状态.
I have a JTable in my JPanel and i want to know how to save the edited cells. What happens right now is that when i edit a cell and press ENTER, the new data is shown.But when I close the program and open it again, the data changes back to what is originally was.
图片:
在编辑JTable时:
While editing the JTable:
重新启动程序后:
预先感谢
我想尝试从项目内的.txt文件中获取数据.我不太确定该怎么做.我不知道如何将保存的数据保存到.txt文件中,或者如何将其实现到JTable中.
I want to try to get the data from a .txt file inside my project. I'm not really sure how to do that. I dont know how to get the saved data into a .txt file or how to implement it into a JTable.
推荐答案
您应该使用TableModelListeners的方法,并在发生任何更改时触发对磁盘的写入(以与加载数据时相同的格式).所以,写类似
You should use the TableModelListeners' methods, and trigger a write to disk (in the same format that you use when loading the data) whenever something changes. So, write something like
myTable.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
// access the values of the model and save them to the file here
}
});
这篇关于Java-如何更改单元格时保存JTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!