JTable:如何从绑定到数据源的表中获取选定的对象

发布于 2024-12-03 04:05:13 字数 394 浏览 6 评论 0原文

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

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

发布评论

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

评论(1

你是我的挚爱i 2024-12-10 04:05:14

不了解 Netbeans,只知道它使用 Beansbinding 的一个版本,因此以下内容肯定可以以某种方式应用

使用绑定框架的整个想法是您永远直接与视图对话,但是完全专注于您的模型(或 bean):此类模型的某些属性绑定到视图的属性,并且您的代码仅侦听 bean 属性的更改。 “SelectedElement”是绑定的人工属性(实际上是 JTableAdapterProvider 的人工属性,但这不是您需要知道的:-),因此将您的模型属性绑定到它 - 这是手动执行此操作的片段:

    // model/bean 
    public class AlbumManagerModel .. {
         // properties
         ObservableList<Album> albums;
         Album selectedAlbum;

         // vents the list of elements
         ObservableList<Album> getManagedAlbums() {
              return albums;
         }

         // getter/setter
         public Album getSelectedAlbum() {
              return selectedAlbum; 
         }

         public void setSelectedAlbum(Album album) {
            Album old = getSelectedAlbum();
            this.selectedAlbum = album;
            firePropertyChange("selectedAlbum", old, getSelectedAlbum());
         }


    }

    // bind the manager to a JTable

    BindingGroup context = new BindingGroup();
    // bind list selected element and elements to albumManagerModel
    JTableBinding tableBinding = SwingBindings.createJTableBinding(
            UpdateStrategy.READ,
            albumManagerModel.getManagedAlbums(), albumTable);
    context.addBinding(tableBinding);
    // bind selection 
    context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            albumManagerModel, BeanProperty.create("selectedAlbum"), 
            albumTable, BeanProperty.create("selectedElement_IGNORE_ADJUSTING")
    ));
    // bind columns 
    tableBinding.addColumnBinding(BeanProperty.create("artist"));
    ...
    context.bind();

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:

    // model/bean 
    public class AlbumManagerModel .. {
         // properties
         ObservableList<Album> albums;
         Album selectedAlbum;

         // vents the list of elements
         ObservableList<Album> getManagedAlbums() {
              return albums;
         }

         // getter/setter
         public Album getSelectedAlbum() {
              return selectedAlbum; 
         }

         public void setSelectedAlbum(Album album) {
            Album old = getSelectedAlbum();
            this.selectedAlbum = album;
            firePropertyChange("selectedAlbum", old, getSelectedAlbum());
         }


    }

    // bind the manager to a JTable

    BindingGroup context = new BindingGroup();
    // bind list selected element and elements to albumManagerModel
    JTableBinding tableBinding = SwingBindings.createJTableBinding(
            UpdateStrategy.READ,
            albumManagerModel.getManagedAlbums(), albumTable);
    context.addBinding(tableBinding);
    // bind selection 
    context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            albumManagerModel, BeanProperty.create("selectedAlbum"), 
            albumTable, BeanProperty.create("selectedElement_IGNORE_ADJUSTING")
    ));
    // bind columns 
    tableBinding.addColumnBinding(BeanProperty.create("artist"));
    ...
    context.bind();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文