问题描述
如果我在 nattable 中排序,则只显示表格中的第一个单元格,并且我得到 NPE:at org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor.getDataValue(ReflectiveColumnPropertyAccessor.java:74)
。所有其他单元格消失。我猜,问题可能出在 composite.setChildLayer(GridRegion.BODY,bodyLayerStack,1);
。 sortHeaderLayer
中应该包含 bodyLayerStack
吗?
例如:
ColumnGroupHeaderLayer columnGroupHeaderLayer =
new ColumnGroupHeaderLayer(sortHeaderLayer,selectionLayer,columnGroupModel);
GridLayer gridLayer =
new GridLayer(viewportLayer,columnGroupHeaderLayer,rowHeaderLayer,cornerLayer);
NatTable natTable = new NatTable(parent,gridLayer,false);
我的代码:
private NatTable createTable(Composite parent,List<TableLine> tLines,String[][] propertyNames,PropertyToLabels[] propToLabels,TableParams params,TextMatcherEditor<TableLine>editor) {
BodyLayerStack bodyLayerStack =
new BodyLayerStack(
tLines,new LineDataProviderColumnAccessor(propertyNames[0].length),params.getColumnIndicesForRowHeaders());
DataLayer bodyDataLayer = bodyLayerStack.getBodyDataLayer();
...
IDataProvider columnHeaderDataProvider =
new DefaultColumnHeaderDataProvider(propertyNames[0],propToLabels[0].getPropertyToLabels());
DataLayer columnHeaderDataLayer =
new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
ILayer columnHeaderLayer =
new ColumnHeaderLayer(
columnHeaderDataLayer,(SelectionLayer)null);
//
SortHeaderLayer<TableLine> sortHeaderLayer =
new SortHeaderLayer<>(
columnHeaderLayer,new GlazedListsSortModel<>(
bodyLayerStack.getSortedList(),new ReflectiveColumnPropertyAccessor<TableLine>(propertyNames[0]),configRegistry,columnHeaderDataLayer));
CompositeLayer composite = new CompositeLayer(1,2);
composite.setChildLayer(GridRegion.COLUMN_HEADER,sortHeaderLayer,0);
composite.setChildLayer(GridRegion.BODY,1);
NatTable natTable = new NatTable(parent,composite,false);
//new CustomTextPainter();
new CustomNatTableContentTooltip(natTable);
// before addConfiguration
natTable.setConfigRegistry(configRegistry);
// to sort on a single click
natTable.addConfiguration(new SingleClickSortConfiguration());
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
natTable.addConfiguration(new ContextMenuConfiguration(natTable));
natTable.addConfiguration(new AbstractRegistryConfiguration() {
});
natTable.configure();
editor.setFilterator(new TextFilterator<TableLine>() {
@Override
public void getFilterStrings(List<String> baseList,TableLine element) {
for( int i = 0; i < element.getLength(); i++ )
baseList.add("" + element.getobjectByColumn(i));
}
});
editor.setMode(TextMatcherEditor.REGULAR_EXPRESSION);
bodyLayerStack.getFilterList().setMatcherEditor(editor);
NatTableSelectionProvider.addNatTableData(natTable,bodyLayerStack.getSelectionLayer(),bodyLayerStack.getBodyDataProvider());
return natTable;
}
我的解决方案是创建 CustomColumnPropertyAccessor,但它仅适用于具有单行标题的列:
public class CustomColumnPropertyAccessor<R extends TableLine> extends ReflectiveColumnPropertyAccessor<R>
{
public CustomColumnPropertyAccessor(String... propertyNames)
{
super(propertyNames);
}
@Override
public Object getDataValue(R rowObj,int columnIndex)
{
return ((TableLine) rowObj).getobjectByColumn(columnIndex);
}
}
对于双行标题,我得到:java.lang.indexoutofboundsexception: Index: 5,Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:657)
感谢您的任何想法
解决方法
所以我从 CustomColumnPropertyAccessor 中删除了 2row 标题表的索引 [0]
所以我得到:
SortHeaderLayer<TableLine> sortHeaderLayer =
new SortHeaderLayer<>(
columnHeaderLayer,new GlazedListsSortModel<>(
bodyLayerStack.getSortedList(),new CustomColumnPropertyAccessor<>(propertyNames),configRegistry,columnHeaderDataLayer));
排序也适用于具有双标题的表格。但是,奇怪的是,如果没有这个 CustomColumnPropertyAccessor,它就无法工作,如示例中所示。也许是因为我不使用网格。