视图排序和过滤:GlazedList 排序和过滤+ JTable vs Glazed 事件列表+JXTable
我想创建具有排序和过滤功能的用户视图(表)。我使用 EventList (Glazed Lists) 作为 EventTableModel 的源。 GlazedLists 中还有排序列表和一些过滤项,因此我可以使用它们来创建视图。但我发现 JXTable 它有排序和过滤的方法,这就是我希望它工作的方式:排序和过滤必须提供 UI 组件和模型才能保存数据:
EventList<Item> source=new BasicEventList<Item>();
TableModel model=new DefaultEventTableModel<Item>(source,tableFormat); // It'll be
//perfect if I could create model without tableFormat,
//because it's presentation of data,
//but in GlazedLists I can't :( ...
JTalbe ui=new JXTable(model); // UI with sorting and filtering
但是 GlazedLists 还提供 SortedList (带有排序的 EventList 的装饰器),并且一些过滤方法。
EventList<Item> source=new BasicEventList<Item>();
SortedList<Item> sortedSource=new SortedList<Item>(source,comparator);
TableModel model=new DefaultEventTableModel<Item>(sortedSource,tableFormat);
// model with sorting... not very beautifull for me, but what do you think?
JTable ui=new JTable(model); // UI with sorting provided by model
问题是:哪种模型更好。或者也许两者都错了,创建视图有什么用呢?
I want to create user views (tables) with sorting and filtering capabilities. I use EventList (Glazed Lists) as source for EventTableModel. There are also Sorted List and some filtering items in GlazedLists, so I can use them for creating view. But I found JXTable and it has methods for sorting and filtering, and this how I want it to work: sorting and filtering must provide UI component and model just can hold data:
EventList<Item> source=new BasicEventList<Item>();
TableModel model=new DefaultEventTableModel<Item>(source,tableFormat); // It'll be
//perfect if I could create model without tableFormat,
//because it's presentation of data,
//but in GlazedLists I can't :( ...
JTalbe ui=new JXTable(model); // UI with sorting and filtering
But GlazedLists also provide SortedList (decorator for EventList with Sorting), and some filtering methods.
EventList<Item> source=new BasicEventList<Item>();
SortedList<Item> sortedSource=new SortedList<Item>(source,comparator);
TableModel model=new DefaultEventTableModel<Item>(sortedSource,tableFormat);
// model with sorting... not very beautifull for me, but what do you think?
JTable ui=new JTable(model); // UI with sorting provided by model
And the question is: what model is better. Or maybe both are wrong, and what do use for creating views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 DefaultEventTableModel 一切都可以。当模型在 EventList 源上注册排序器时。它是在内部完成的,如您的第二个清单中所示。如果您手动注册排序器,则必须实现所有代码才能更改比较器(例如 asc、desc)。当尝试使用过滤时,它是相同的,但 JXTable 恕我直言不支持此机制,因此您必须以自己的方式执行此操作。
In case you use the DefaultEventTableModel everthing is ok. As the model registers sorters on the EventList source. It's done internally as in your 2nd listing. In case you register the sorters manually, then you have to implement all the code in order to change the comparator (e.g. asc, desc). When trying to use filtering its the same, but the JXTable imho doesn't support a mechanism for this, so you have to do this in your own way.
我投票给 GlazedLists,因为它有效。是的,与表一起使用的 TableModel 与视图紧密耦合,但是您将该表模型与源数据解耦。
GlazedLists 的排序和过滤功能比 JXTable 上的灵活得多。只需确保您没有同时打开它们,否则事情会变得混乱。下面是我在 JXTable 中使用 SortedList 的常用代码片段:
作用:
请注意,您传入两个 EventList、一个displayItems(管道末尾的列表)以及一个用于控制使用哪一列的sortedList用于排序,它在管道中可以早于 displayItems 列表。 (如果你的最后一个元素是sortedList,之后不进行任何处理,只需将列表传递两次即可。)
I vote for GlazedLists, because it works. Yes, the TableModel that you use with the table is tightly coupled to the view, but you decouple that table model from your source data.
GlazedLists' sorting and filtering features are much more flexible than the one on JXTable. Just make sure you don't have them both turned on, or things will get confusing. Here's my usual code snippet for using a SortedList with a JXTable:
What this does:
Note that you pass in two EventLists, a displayItems which is the list at the end of the pipeline, and a sortedList used for controlling which column is used for sorting, which can be earlier in the pipeline than the displayItems list. (If your last element is the sortedList, without any processing after that, just pass the list in twice.)