[英]Dynamically change row color in NatTable
我試圖根據行條件更改背景行顏色。 我離得很近,但是有些事我不能完全按一下。 (我相信這是因為我要從基礎列表中拉一個對象,而不是動態獲取數據。我在下面的代碼部分做了標記)
在下面的示例中,每行顏色均基於具有成功或失敗值的對象(MyObj)。 如果myObj具有成功值,則該行應為綠色。 如果myObj具有失敗值,則該行應為紅色。 如果myObj沒有值,則應使用默認行顏色。
當我運行代碼時,行顏色將按預期顯示。 但是,如果我對列進行排序,則當數據移動到新的行索引時,原始行索引將保持該顏色。 我希望行顏色將隨對象一起移動,而不是始終固定在該行索引處。
Example:
Row 1 - "SUCCESS" - Shows Green
Row 2 - "FAIL" - Shows Red
如果按字母順序對該列進行排序,則會得到:
Row 1 - "FAIL - Shows Green
Row 2 - "SUCCESS" - Shows Red
下面是我用來生成示例的代碼片段:
void example() {
getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
Style cellStyleSuccess = new Style();
cellStyleSuccess.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR,
COLOR_SUCCESS);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE,
cellStyleSuccess,
DisplayMode.NORMAL, "SUCCESS");
Style cellStyleFail = new Style();
cellStyleFail.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR,
COLOR_FAILURE);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE,
cellStyleFail,
DisplayMode.NORMAL, "FAIL");
}
});
DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
IConfigLabelAccumulator cellLabelAccumulator =
new IConfigLabelAccumulator() {
@Override
public void accumulateConfigLabels(LabelStack configLabels,
int columnPosition, int rowPosition) {
configLabels.getLabels().clear();
// TODO Is this the issue? Is there a better way to
// pull MyObj here?
MyObj myObj = getEventList().get(rowPosition);
if (myObj.getFoo().equals("SUCCESS")) {
configLabels.addLabel("SUCCESS");
} else if (myObj.getFoo().equals("FAIL"))) {
configLabels.addLabel("FAIL");
} else {
// default color
}
}
};
dl.setConfigLabelAccumulator(cellLabelAccumulator);
getNatTable().configure();
}
可能導致問題的重要部分丟失了。 getEventList()
返回哪個列表? 如果它是基本的EventList
,則始終在原始索引處獲取該對象。 排序時,將通過SortedList
應用SortedList
。 因此,如果getEventList()
返回最頂級的GlazedLists集合(取決於所使用的功能是SortedList
還是FilterList
getEventList()
則應該解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.