【问题标题】:Is there a way to highlight a row in GlazedLists?有没有办法突出显示 GlazedLists 中的一行?
【发布时间】:2015-01-16 14:09:50
【问题描述】:

我有一个列表,用于监控严格升序的数字序列中某些实体的到达,并希望在序列中有明显中断的地方显示一个条目。

有没有办法突出显示GlazeLists 中的条目?

【问题讨论】:

    标签: java glazedlists


    【解决方案1】:

    很难确定您是在询问如何突出显示列表中的新元素,还是字面上突出显示由 GlazedLists EventList 支持的 UI 组件中的一行。

    现在我假设前者,但请随时澄清。

    GlazedLists 包中有ListEvents 的概念,它允许人们在影响列表的更改中获得一个小峰值。这不是我玩过太多的东西,而且看起来很简陋,但在适当的情况下可以使用这种机制。

    这是一个示例类,它有一个包含一些整数的BasicEventList。我创建了一个ListEventListener 并将其附加到EventList。 ListEvents 告诉您元素插入的位置。它还包含对事件列表的引用,因此可以获得新插入的值,以及它之前的元素的值。我做了一个快速比较,看看它们是否乱序。

    当然,这里有一些主要的警告。事件处理是异步的,因此在原始触发器的时间和侦听器处理事件的时间之间,底层列表完全有可能发生很大变化。在我的示例中没关系,因为我只使用附加操作。另外我只使用BasicEventList;如果它是SortedList,那么这些项目将被插入到不同的索引中,所以我用来获取当前值和以前值的方法将非常不可靠。 (可能有办法解决这个问题,但老实说,我并没有把自己应用于这个问题。)

    至少您可以使用侦听器至少提醒您列表更改,并让侦听器类之外的另一个方法执行对列表的扫描以确定是否有项目乱序。

    import ca.odell.glazedlists.BasicEventList;
    import ca.odell.glazedlists.EventList;
    import ca.odell.glazedlists.GlazedLists;
    import ca.odell.glazedlists.event.ListEvent;
    import ca.odell.glazedlists.event.ListEventListener;
    
    public class GlazedListListen {
    
        private final EventList<Integer> numbers = new BasicEventList<Integer>();
    
        public GlazedListListen() {
    
            numbers.addListEventListener(new MyEventListListener());
    
            numbers.addAll(GlazedLists.eventListOf(1,2,4,5,7,8));
    
        }
    
        class MyEventListListener implements ListEventListener<Integer> {
            @Override
            public void listChanged(ListEvent<Integer> le) {
    
                while (le.next()) {
                    if (le.getType() == ListEvent.INSERT) {
                        final int startIndex = le.getBlockStartIndex();
                        if (startIndex == 0) continue; // Inserted at head of list - nothing to compare with to move on.
    
                        final Integer previousValue = le.getSourceList().get(startIndex-1);
                        final Integer newValue = le.getSourceList().get(startIndex);
                        System.out.println("INSERTING " + newValue + " at " + startIndex);
                        if ((newValue - previousValue) > 1) {
                            System.out.println("VALUE OUT OF SEQUENCE! " + newValue + " @ " + startIndex);
                        }
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            new GlazedListListen();
        }
    }
    

    注意:我只针对 GlazedLists v1.8 进行了测试。

    【讨论】:

    • 是的,这是我想以某种方式突出显示(或可能为它着色)的新元素..感谢您的代码..我会看看我如何得到反馈..
    猜你喜欢
    • 2021-03-31
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多