ekjcfn3902039 ekjcfn3902039 - 9 months ago 58
Java Question

Adding Image and background to NatTable rows

I have read the documentation at: https://eclipse.org/nattable/documentation.php?page=styling

I am curious if there is any easy way to add background row colors and images using separate configurations. I do not wish to combine them into 1 configuration like the CellPainterWrapper example because I want to separate the logic between the two. My current code works for either the image or the background color, but I cannot do both (the top-most configuration overrides the bottom-most one). Below is my snippet:

void run(){
addBackgroundRowColors();
addImageToColumn();
}

void addImageToColumn() {
getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
final Style cellStyleOne = new Style();
cellStyleOne.setAttributeValue(CellStyleAttributes.IMAGE,
myIcon);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE, cellStyleOne,
DisplayMode.NORMAL, myIconLabel);

final Style cellStyleTwo = new Style();
cellStyleTwo.setAttributeValue(CellStyleAttributes.IMAGE,
myIcon2);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE, cellStyleTwo,
DisplayMode.NORMAL, myIconLabel2);

configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_PAINTER,
new CellPainterDecorator(new TextPainter(),
CellEdgeEnum.LEFT, 10, new ImagePainter()),
DisplayMode.NORMAL);
}
});
DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
IConfigLabelAccumulator cellLabelAccumulator = (configLabels,
columnPosition, rowPosition) -> {
// Label code here...
};

dl.setConfigLabelAccumulator(cellLabelAccumulator);
}

void addBackgroundRowColors() {
getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
Style cellStyleOne = new Style();
cellStyleOne.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR, myColorOne);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE, cellStyleOne,
DisplayMode.NORMAL, myColorLabel1);

Style cellStyleTwo = new Style();
cellStyleTwo.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR, cellStyleTwo);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE, cellStyleTwo,
DisplayMode.NORMAL, myColorLabel2);
}
});
DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
IConfigLabelAccumulator cellLabelAccumulator = (configLabels,
columnPosition, rowPosition) -> {
// Label code here...
};

dl.setConfigLabelAccumulator(cellLabelAccumulator);
}


Update



I ended up doing something similar to the following to make it work:

AggregateConfigLabelAccumulator aggregate =
new AggregateConfigLabelAccumulator();
aggregate.add(addImageToColumn());
aggregate.add(addBackgroundRowColors());

getGlazedListsGridLayer().getBodyDataLayer().
setConfigLabelAccumulator(aggregate);

Answer Source

From the comments the real question is about how to support separated IConfigLabelAccumulator. As you can only register one IConfigLabelAccumulator per layer, there are two ways to achieve this:

  1. register the different IConfigLabelAccumulator on different layers
  2. Use an AggregateConfigLabelAccumulator where you can combine multiple IConfigLabelAccumulator

This is also explained in the Getting Started Tutorial: http://www.vogella.com/tutorials/NatTable/article.html