通过 AbstractTableModel 获取选定行
是否可以从我的表模型中获取选定的行索引?
我的对象已经知道表模型。 我可以使用模型获取选定的索引,而不是传递对表本身的引用吗?
Is it possible to get the selected row index from my table model?
My object already knows about the table model. Instead of passing a reference to the table it self can i get the selected index using the model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从绑定的表中获取索引,然后可以使用它来操作表模型。 例如,如果我想删除表模型中的一行:
You can get the index from the bound Table and then you can use it to manipulate the table model. For example, if I want to delete a Row in my table model:
就像 MrWiggles 所说,您可以从 ListSelectionModel 中获取它,您可以从表本身访问它。 然而,JTable 中也有一些方便的方法来获取选定的行。 如果您的表是可排序的等,您还需要执行convertRowIndexToModel方法:)
来自JTable JavaDoc:
Like MrWiggles said you can get it from the ListSelectionModel which you is accessible from the table itself. However there are convenience methods in JTable to get the selected rows as well. If your table is sortable etc you will also need to go through the convertRowIndexToModel method :)
From the JTable JavaDoc:
TableModel 只关心数据,ListSelectionModel 关心当前选择的内容,所以,你无法从 TableModel 获取所选行。
The TableModel only concerns itself with the data, the ListSelectionModel concerns itself with what is currently selected, so, no you can't get the selected row from the TableModel.
如果你让你的模型类实现 ListSelectionModel 以及 TableModel,你将能够从一个模型中进行选择...但你不能扩展两个抽象模型类:-((无论如何,这也不是一个好主意,因为你的类将会承担太多的责任)。
If you let your model class implement ListSelectionModel as well as TableModel, you will be able to get selection from one model... but you cannot extend two abstract model classes :-( (It also isn't very good idea anyway as your class will have too many responsibilities).