通过 AbstractTableModel 获取选定行

发布于 2024-07-20 04:28:05 字数 82 浏览 3 评论 0原文

是否可以从我的表模型中获取选定的行索引?

我的对象已经知道表模型。 我可以使用模型获取选定的索引,而不是传递对表本身的引用吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟燃烟灭 2024-07-27 04:28:06

您可以从绑定的表中获取索引,然后可以使用它来操作表模型。 例如,如果我想删除表模型中的一行:

myTableModel.removeValueAt(myTable.getSelectedRow());

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:

myTableModel.removeValueAt(myTable.getSelectedRow());
自此以后,行同陌路 2024-07-27 04:28:05

就像 MrWiggles 所说,您可以从 ListSelectionModel 中获取它,您可以从表本身访问它。 然而,JTable 中也有一些方便的方法来获取选定的行。 如果您的表是可排序的等,您还需要执行convertRowIndexToModel方法:)

来自JTable JavaDoc:

   int[] selection = table.getSelectedRows();
   for (int i = 0; i < selection.length; i++) {
     selection[i] = table.convertRowIndexToModel(selection[i]);
   }
   // selection is now in terms of the underlying TableModel

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:

   int[] selection = table.getSelectedRows();
   for (int i = 0; i < selection.length; i++) {
     selection[i] = table.convertRowIndexToModel(selection[i]);
   }
   // selection is now in terms of the underlying TableModel
早乙女 2024-07-27 04:28:05

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.

情绪少女 2024-07-27 04:28:05

如果你让你的模型类实现 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文