JTable:如何从绑定到数据源的表中获取选定的对象
我有 JTable,其中“元素”属性绑定到对象列表,这是主表。还有详细信息表,其中“elements”属性绑定到主表中的 selectedElement。我是在 NetBeans GUI 构建器的帮助下完成的。现在我尝试得到这样的东西:
SomeEntityType selectedObject= (SomeEntityType) masterTable.getSelectedElement ()
在源代码中,但是JTable中没有这样的属性,只有“getSelectedRow”。那么,如何从绑定到源(对象列表)的 JTable 中获取选定的对象? 我读过类似的问题,但只找到 getValueAt(rowId,columnId) 方法上的链接,但在我的任务中,选择哪一列并不重要,因为选择了整行。
I have JTable which "elements" property binded to List of objects, this is master table. There is also details table, which "elements" property binded to selectedElement in master table. I did it with help of NetBeans GUI builder. Now I try to get something like this:
SomeEntityType selectedObject= (SomeEntityType) masterTable.getSelectedElement ()
in source code, but there is no such property in JTable, only "getSelectedRow". So, how can I get selected object from JTable binded to source (List of objects)?
I have read similar questions, but find only link on getValueAt(rowId,columnId) method, but in my task it doesn't matter which column is selected, because full row is selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不了解 Netbeans,只知道它使用 Beansbinding 的一个版本,因此以下内容肯定可以以某种方式应用
使用绑定框架的整个想法是您永远直接与视图对话,但是完全专注于您的模型(或 bean):此类模型的某些属性绑定到视图的属性,并且您的代码仅侦听 bean 属性的更改。 “SelectedElement”是绑定的人工属性(实际上是 JTableAdapterProvider 的人工属性,但这不是您需要知道的:-),因此将您的模型属性绑定到它 - 这是手动执行此操作的片段:
don't know about Netbeans, just know that it uses a version of Beansbinding, so the following certainly can applied somehow
The whole idea of using a binding framework is that you never directly talk to the view, but fully concentrate on your model (or bean): some property of such a model is bound to a property of a view and your code only listens to changes in the the properties of your bean. "SelectedElement" is an artificial property of the binding (actually, of the JTableAdapterProvider, but that's nothing you need to know :-), so bind your model property to that - here's a snippet of doing so manually: