Nattable 为特定列的不同值添加不同Label的两种方法

时间:2021-03-02 19:16:58

Option 1:

第一步: 实例化一个CellOverrideLabelAccumulator 对象, 调用registerOverride 方法。

代码示例:

CellOverrideLabelAccumulator<youRow> cellLabelAccumulator = new CellOverrideLabelAccumulator<>(bodyDataProvider);
cellLabelAccumulator.registerOverride(value1, 1, label1);  // the second parameter is the column position, 1 means the second column
cellLabelAccumulator.registerOverride(value2, 1, label2);
bodyDataLayer.setConfigLabelAccumulator(cellLabelAccumulator);

 

第二步: 实例化一个configuration, 并添加到Nattable 实例。

class YourConfiguration extends AbstractRegistryConfiguration {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
             // Your custom registry code here , should involve the label defined in Step 1
        }

}

 

YourConfiguration yourConfiguration = new YourConfiguration();
natTable.addConfiguration(yourConfiguration); 

Option 2:

第一步: 从CellOverrideLabelAccumulator 派生出一个自定义的子类,重写accumulateConfigLabels 方法

class YourLabelAccumalator extends CellOverrideLabelAccumulator<YourRow> {
        private ListDataProvider<YourRow> thisDataProvider;

        public YourLabelAccumalator(ListDataProvider<YourRow> dataProvider) {
            super(dataProvider);
            thisDataProvider = dataProvider;
        }

        @Override
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
            
            // should check the column position first
            if (columnPosition == 1) {
                Object value=  this.thisDataProvider.getDataValue(1, rowPosition);
                
                if (value== value1) {
                    configLabels.addLabel(label1);  // should defined label1 as String
                } else if (value== value2) {
                    configLabels.addLabel(label2); //  same as above
                }
            }
            
        }
    }

 

第二步: 实例化一个configuration, 并添加到Nattable 实例 (同Option 1 的第二步一样)