javanattable

NatTable: colored row selected, what about the selected cell?


I have a NatTable and some colored rows, via the label "mylabel".
"mylabel" is assigned by a ConfigLabelAccumulator:

final AggregateConfigLabelAccumulator labelAccumulator = new AggregateConfigLabelAccumulator();
labelAccumulator.add(new ColumnLabelAccumulator());
labelAccumulator.add(new IConfigLabelAccumulator() {
    @Override
    public void accumulateConfigLabels(final LabelStack configLabels, final int columnPosition, final int rowPosition) {
        if (<my condition>) configLabels.addLabelOnTop("mylabel");
    }
});

Styles for "mylabel" are assigned via ConfigRegistry, "YELLOW" for unselected rows, "DARK_YELLOW" for selected rows:

final ConfigRegistry configRegistry = new ConfigRegistry();
final Style style = new Style();
style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_YELLOW);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, "mylabel");

final Style styleSelected = new Style();
styleSelected.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, Display.getDefault().getSystemColor(SWT.COLOR_DARK_YELLOW));
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, styleSelected, DisplayMode.SELECT, "mylabel");

(sidenote) After the condition (see <my condition>) changes, I do natTable.doCommand(new VisualRefreshCommand()); to instantly refresh the table.

It works like a charm, but for one thing: The selected cell!
How can I tell the selected cell to have a different color when <my condition> is true?

Example pictures:
Both rows are selected in both pictures (=> dark yellow), only the selection anchor is different.

want to color this
The cell containing 529... should have a different style when selected.

want to stay like it is
The cell containing /E0001 should stay like it is.

Thank you very much, Dirk !!!

I ended up with this solution, tweaking the SelectionLayer's DefaultSelectionLayerConfiguration and DefaultSelectionStyleConfiguration:

this.selectionLayer = new SelectionLayer(glazedListsEventLayer, false);
this.selectionLayer.addConfiguration(new DefaultSelectionLayerConfiguration() {
    @Override
    protected void addSelectionStyleConfig() {
        final DefaultSelectionStyleConfiguration dssc = new DefaultSelectionStyleConfiguration();
        dssc.anchorBgColor = null;
        dssc.anchorFgColor = null;
        dssc.anchorBorderStyle = new BorderStyle(1, GUIHelper.COLOR_RED, LineStyleEnum.SOLID);
        addConfiguration(dssc);
    }
});

Solution

  • IIUC you are talking about the cell that has the focus in a selected row/column. That is called the selection anchor. And the selection anchor is styled specifically via the label SelectionStyleLabels.SELECTION_ANCHOR_STYLE to distinguish the selected cell that has the focus from other selected cells in a multi selection scenario.

    That said, you need to configure the style for the selection anchor additionally. But as it is not possible to configure styles for multi-labels, the only approach I know is to remove the background styling for the selection anchor so the background color is inherited from the general selection style. And if you want to highlight the selection anchor, use some other style bit, e.g. setting the border.

    IStyle anchorStyle = new Style();
    anchorStyle.setAttributeValue(
            CellStyleAttributes.BORDER_STYLE,
            new BorderStyle(1, GUIHelper.COLOR_RED, LineStyleEnum.SOLID));
    configRegistry.registerConfigAttribute(
            CellConfigAttributes.CELL_STYLE,
            anchorStyle,
            DisplayMode.SELECT,
            SelectionStyleLabels.SELECTION_ANCHOR_STYLE);