JTable 跟踪用户何时更改单元格并且仅跟踪用户
我正在开发我的第一个 Java/Swing 应用程序,并且需要一些有关 JTable 的帮助。
我遇到的问题是我想跟踪用户何时对我的 JTable 而不是应用程序进行编辑。我做了以下操作:
//Made my table then did the following line:
table.getModel().addTableModelListener(this);
//Have a listener set up:
public void tableChanged(TableModelEvent e) {
// TODO Auto-generated method stub
System.out.println(e);
}
现在的问题是,当我的程序对表进行更改时,我不希望触发此事件,例如,我加载一个新表,并且加载的文件有 15 行,那么我会触发 15 个不同的事件。我只想在用户对单元格进行编辑时触发。
任何帮助表示赞赏。提前致谢。
I am working on my first Java/Swing application and I need some help with the JTable.
The problem I am having is that I want to track when a USER makes edits to my JTable and NOT the application. I did the following:
//Made my table then did the following line:
table.getModel().addTableModelListener(this);
//Have a listener set up:
public void tableChanged(TableModelEvent e) {
// TODO Auto-generated method stub
System.out.println(e);
}
Now the problem is I do not want this to fire when my program does changes to the Table, for example I load a new table and the file I loaded had 15 rows then I would get 15 different events firing. I only want to fire an even when the USER made an edit to a cell.
Any help is appreciated. Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
安德鲁的评论提供了一些处理这种情况的最常见方法。根据您的模型的构建/存储方式以及您想要对更改执行的操作,您可能会在更改时在模型中触发某些内容,或者可能在 setValueAt 方法下触发某些内容。
Andrew's comment has some of the most common ways to handle this situation. Depending on how your model is constructed/stored and what exactly you want to do on a change you could have something fire in the model when changed, or possible under the setValueAt method.