2016-11-17 38 views
0

我正在查看(509_SortHeaderLayer.java)示例作为参考点。NatTable使用自定义比较器排序

我将一个自定义比较器直接添加到SortedList中,如下例所示。但是,当我单击调试器中的列时,我的自定义比较器从未到达我在compare()方法的第一行中设置的断点。

如果我将比较器作为AbstractRegistryConfiguration添加,它将按预期工作(点击某列时会达到断点)。

为什么在SortedLists构造函数中设置比较器不能像我期望的那样工作?一些通用的代码片段如下所示:

public void setSortComparatorWorks() { 
    SortedList<T> sortedList = new SortedList<>(eventList, null); 
    init(sortedList); 
    getNatTable().addConfiguration(new AbstractRegistryConfiguration() { 

     @Override 
     public void configureRegistry(IConfigRegistry configRegistry) { 
      configRegistry.registerConfigAttribute(SortConfigAttributes. 
       SORT_COMPARATOR, new MyComparator<T>(), 
       DisplayMode.NORMAL); 
     } 
    }); 
    getNatTable().configure(); 
} 

public void setSortComparatorDoesntWork() { 
    SortedList<T> sortedList = new SortedList<>(eventList, 
     new MyComparator<T>); 
    init(sortedList); 
    getNatTable().configure(); 
} 

private void init(SortedList sortedList){ 
    this.bodyDataProvider = new ListDataProvider<>(sortedList, 
     columnPropertyAccessor); 

    this.bodyDataLayer = new DataLayer(this.bodyDataProvider); 

    this.bodyLayerStack = new DefaultBodyLayerStack(new 
     GlazedListsEventLayer<>(this.bodyDataLayer, eventList)); 

    this.columnHeaderLayerStack = new 
     GlazedListsColumnHeaderLayerStack<>(
     columnHeaderDataProvider, sortedList, 
     columnPropertyAccessor, configRegistry, this.bodyLayerStack); 

    this.sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayerStack, 
     new GlazedListsSortModel<T>(sortedList, 
     columnPropertyAccessor, configRegistry, bodyDataLayer), 
     false); 

    setChildLayer(GridRegion.COLUMN_HEADER, sortHeaderLayer, 0, 0); 
    setChildLayer(GridRegion.BODY, bodyLayerStack, 0, 1); 

    getNatTable().addConfiguration(new SingleClickSortConfiguration()); 
} 

回答

1

您预期的那样不起作用,因为内部功能将在SortedList与推导出ConfigRegistry和当前应用的种类的Comparator替换现有Comparator州。

顺便说一句,有趣的是,你参考_509_SortHeaderLayerExample而GlazedLists的例子是_602_GlazedListsSortingExample

+0

我有点困惑......如果我在构造函数中添加比较器,并且我没有向ConfigRegistry添加一个,为什么它仍然不起作用?这在上面的setSortComparatorDoesntWork方法的xample中得到了演示。我没有看到602的例子,我也会检查。 – ekjcfn3902039

+0

正如我已经说过,它会被派生出来。即使你自己没有设置,你也可以使用SingleClickSortConfiguration(它是一个DefaultSortConfiguration)来添加一个。 –

相关问题