Nattable 爲特定列的不一樣值添加不一樣Label的兩種方法

Option 1:java

第一步: 實例化一個CellOverrideLabelAccumulator 對象, 調用registerOverride 方法。ide

代碼示例:this

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 實例。code

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 方法blog

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 的第二步同樣)get

相關文章
相關標籤/搜索