简体   繁体   English

SortController:如何使用动态过滤器

[英]SortController: how to use a dynamic filter

The JXTable from the SwingX project has a method to set a RowFilter directly on the JXTable (see JXTable#setRowFilter ). SwingX 项目中的JXTable有一个方法可以直接在JXTable上设置RowFilter (参见JXTable#setRowFilter )。

It is unclear to me what you need to do when the filter implementation is dynamic.我不清楚当过滤器实现是动态的时你需要做什么。

To compare: in the JDK they introduced the RowSorter interface in JDK1.6, and this interface allows to attach a RowSorterListener .比较:在JDK1.6中引入了RowSorter接口,该接口允许附加一个RowSorterListener If you have a dynamic RowSorter (which can also be used for filtering), you can fire an event to those listeners when the filter changes (although this requires that the filter has access to the RowSorter as there is no such thing as a DynamicRowFilter or RowFilterEvent in the JDK).如果您有动态RowSorter (也可用于过滤),您可以在过滤器更改时向这些侦听器触发事件​​(尽管这要求过滤器有权访问RowSorter因为没有DynamicRowFilter或JDK 中的RowFilterEvent )。

What I would expect is that I can set an "observable" RowFilter on the JXTable and that I have to do nothing.我期望的是我可以在JXTable上设置一个“可观察的” RowFilter ,而我什么都不用做。 But as that seems not to be possible with the current API, what is the next best thing ?但是由于当前的 API 似乎无法实现,那么下一个最好的事情是什么?

  • set the filter again each time it changes.每次更改时重新设置过滤器。 Will this even have effect as it is the exact same instance as was previously set on the JXTable ?这是否会产生影响,因为它与之前在JXTable上设置的实例JXTable
  • abuse the table events and just fire an event that your table model has been changed, which will retrigger the sorting/filtering滥用表事件并触发表模型已更改的事件,这将重新触发排序/过滤
  • do not use the setRowFilter method and create your own TableRowSorter and fall back on the JDK API.不要使用setRowFilter方法并创建自己的TableRowSorter并依靠 JDK API。

The use-case we have is a filter (not a rowfilter) which can be changed to filter out elements of a model, and we have a RowFilter based on that dynamic filter to filter out those elements我们拥有的用例是一个过滤器(不是 rowfilter),它可以更改以过滤掉模型的元素,并且我们有一个基于该动态过滤器的 RowFilter 来过滤掉这些元素

Repeating my comment: the RowFilter must be immutable.重复我的评论:RowFilter 必须是不可变的。 That was a concious design decision back when sorting/filtering was introduced into core.当排序/过滤被引入核心时,这是一个有意识的设计决定。 So the approach to implement "dynamic" filtering is to所以实现“动态”过滤的方法是

  • make your custom filter (not a rowFilter) observable使您的自定义过滤器(不是 rowFilter)可观察
  • implement a listener to that filter which creates a new RowFilter on changes实现该过滤器的侦听器,该过滤器在更改时创建新的 RowFilter
  • set the rowFilter to the xtable (in SwingX) or to the DefaultRowSorter (in core)将 rowFilter 设置为 xtable(在 SwingX 中)或 DefaultRowSorter(在核心中)

Edit编辑

I don't agree with that design, but couldn't sway him我不同意那个设计,但不能动摇他

should be: I didn't agree - meanwhile, I'm not so sure as I had been ;-)应该是:我不同意- 同时,我不像以前那样确定 ;-)

The advantage of this approach is that the RowFilter is really small coin to implement and highly re-usable - just a simple predicate, nothing else.这种方法的优点是 RowFilter 实现起来真的很小,而且高度可重用——只是一个简单的谓词,没有别的。 That allows simple logic compounding (and/or) of filters.这允许过滤器的简单逻辑复合(和/或)。 No burden to notify on part of the filter, no burden on part of the sorter (or compound filter) to listen and update itself.没有负担通知部分过滤器,也没有负担分拣机(或复合过滤器)的一部分来监听和更新自己。 Then taking into account that the "dynamics" of the filter change often comes from user interaction and something has to listen to those user triggers anyway, it's not a big deal to create a new rowFilter vs. updating an existing rowFilter.再考虑到该更换过滤器的“动态”往往来自于用户交互和东西已经听那些用户触发,无论如何,这不是一个大问题,以创建一个新的RowFilter与更新现有的RowFilter。

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

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