[英]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.