简体   繁体   English

如何从JTable检索“ $ {selectedElement}”,在JTable中将元素绑定到List

[英]How to retrieve “${selectedElement}” from a JTable where the elements are bound to a List

In my application I have a JTable and a List. 在我的应用程序中,我有一个JTable和一个List。

  • The List : The list is populated using a JPA query. 列表 :使用JPA查询填充列表。 The user can re-execute the query by changing attributes in the GUI. 用户可以通过更改GUI中的属性来重新执行查询。 Let's assume the query has a named parameter "year" and the user can change this. 假设查询具有命名参数“ year”,并且用户可以更改此参数。 Then the following happens (simplified by leaving out exception handling): 然后发生以下情况(通过省略异常处理来简化):

     myList.clear() mylist.addAll( myQuery.setParameter("year", 2010) ) 

    As the list changes, the binding fires the required handlers and the table now reflects the new dataset. 随着列表的更改,绑定将触发所需的处理程序,并且该表现在反映了新的数据集。

  • The JTable : JTable

    The contents of the JTable come from a BeansBinding (more precisely a JTableBinding ). JTable的内容来自BeansBinding(更确切地说是JTableBinding )。 The binding source is the aforementioned list. 绑定源是上述列表。

The query is only executed for intensive tasks. 该查询仅对密集型任务执行。 Like applying a rough filter on a huge dataset. 就像在庞大的数据集上应用粗略的过滤器一样。 The earlier example with the year is a good example. 带有年份的较早示例是一个很好的示例。 This will always return a manageable chunk of data to the client. 这将始终将可管理的数据块返回给客户端。 Now, to have a more responsive user experience, more fine-grained filters happen in the JTable itself. 现在,为了获得更敏感的用户体验,JTable本身会进行更细粒度的筛选。 This avoids unnecessary round-trips to the database. 这样可以避免不必要的数据库往返。

Next, assume the following scenario: A user selects a row in the table and hit's the delete button. 接下来,假定以下情形:用户在表中选择一行,然后单击“删除”按钮。 If the table has not been filtered the required code is straightforward (again, no error-checking, concurrency locking, and exception handlers for code simplicity): 如果过滤表,则所需的代码将很简单(再次,为了简化代码,不进行错误检查,并发锁定和异常处理程序):

MyObject = myList.get( myTable.getSelectedRow() );
myEntityManager.getTransaction().begin()
myEntityManager.remove( myObject )
myEntityManager.getTransaction().commit()

However : If the table is filtered on the client side, the table won't reflect the data inside the List. 但是 :如果在客户端对表进行过滤,则该表将不会反映列表中的数据。 So getSelectedRow() will not return an index which will map to the same entry in the List ( I have not tested this, but I beleive I am correct with this assumption? ) 所以getSelectedRow()将不会返回索引,该索引将映射到List中的相同条目( 我没有测试过,但是我相信我对这个假设是正确的吗?

So... My question: 所以...我的问题:

How best to solve this? 如何最好地解决这个问题?

A possible solution? 可能的解决方案?

I've solved the problem at hand with the following: 我已经解决了以下问题:

  • I created a new Bean called selectedTableElement which contains a member holding the element which is currently selected in the table. 我创建了一个名为selectedTableElement的新Bean,其中包含一个成员,该成员包含表中当前选定的元素。
  • Next, I created a new binding (source: my table, target: my "selectedElement" bean) using 接下来,我使用以下命令创建了一个新的绑定(源:我的表,目标:我的“ selectedElement” bean)

     binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, myTable, ELProperty.create("${selectedElement}"), selectedTableElement, BeanProperty.create("selectedElement"), "selectedElementBinding"); 

This solution effectively solves the problem by keeping track of the selected element of the table using Beans Binding. 该解决方案通过使用Beans Binding跟踪表的选定元素来有效地解决了该问题。

But is this really necessary? 但这真的有必要吗? It feels clunky to me. 我觉得笨拙。 A whole new class only the encapsulate the selected element? 一个全新的类仅封装所选元素吗? Is there no other, more direct way of retrieving the "${selectedElement}" property of the JTable ? 是否没有其他更直接的方法来检索JTable"${selectedElement}"属性?

I'll try to answer to both your questions. 我会尽力回答您的两个问题。
Regarding the first question (the filtered selected index vs the real list index): 关于第一个问题(筛选出的选定索引与实际列表索引):

  • I assume that you bound the table using BeansBinding, via createJTableBinding . 我假设您通过createJTableBinding使用BeansBinding绑定了表。 So, the client-side filtering might be applied through the use of swing TableRowSorter and RowFilter. 因此,可以通过使用swing TableRowSorter和RowFilter来应用客户端过滤。 Am I right ? 我对吗 ? if so, you could use the method 如果是这样,您可以使用该方法
    int row = myTable.convertRowIndexToModel(myTable.getSelectedRow());
    to convert the selected row in the filtered view to the actual selected row in the model. 将过滤视图中的选定行转换为模型中的实际选定行。

For the second question (bean to keep the selected item of table) 对于第二个问题(bean保留表中的选定项)

  • You could also create a binding using this as the source/target object, and create a property selectedElement in the class containing the table. 您还可以使用this对象作为源/目标对象来创建绑定,并在包含表的类中创建属性selectedElement Thus, you won't need another class. 因此,您不需要其他课程。 The code will be : createAutoBinding(UpdateStrategy.READ_WRITE, myTable,
    ELProperty.create("${selectedElement}"), this, BeanProperty.create("selectedElement"), "selectedElementBinding");
    该代码将是: createAutoBinding(UpdateStrategy.READ_WRITE, myTable,
    ELProperty.create("${selectedElement}"), this, BeanProperty.create("selectedElement"), "selectedElementBinding");
    createAutoBinding(UpdateStrategy.READ_WRITE, myTable,
    ELProperty.create("${selectedElement}"), this, BeanProperty.create("selectedElement"), "selectedElementBinding");

    (note also that READ_WRITE binding is not really used, as beans binding does not support changing the selected element from the bound property) (还要注意,实际上并没有使用READ_WRITE绑定,因为bean绑定不支持从bind属性中更改所选元素)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM