Home » Eclipse Projects » NatTable » Background color in a cell
Background color in a cell [message #1771448] |
Fri, 25 August 2017 07:10 |
Angel Fraile Messages: 10 Registered: June 2016 |
Junior Member |
|
|
Hi, I want to change the cell color to light blue by selecting a cell. I used BackgroundPainter but I lost the cell content by clicking. Here is my code:
natTableConfReg.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new myPainter(), DisplayMode.SELECT, columnLabel);
private class myPainter extends BackgroundPainter {
@Override
public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {
if (getSelectedTableCellsPositions().size() == 1) {
Color originalBackground = gc.getBackground();
gc.setBackground(new Color(null, 215, 240, 255));
gc.fillRectangle(bounds);
gc.setBackground(originalBackground);
} else {
super.paintCell(cell, gc, bounds, configRegistry);
}
}
}
What is necessary to do, not to lose the cell string content? Thanks in advance
[Updated on: Fri, 25 August 2017 07:15] Report message to a moderator
|
|
|
Re: Background color in a cell [message #1771451 is a reply to message #1771448] |
Fri, 25 August 2017 07:20 |
Dirk Fauth Messages: 2902 Registered: July 2012 |
Senior Member |
|
|
Why do you register a custom painter when you only need to register the appropriate style?
Simply add the below to your configuration instead of the custom painter.
// default selection style
IStyle selectionCellStyle = new Style();
selectionCellStyle.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR,
new Color(null, 215, 240, 255));
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE,
selectionCellStyle,
DisplayMode.SELECT);
// selection anchor
IStyle selectionAnchorStyle = new Style();
selectionAnchorStyle.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR,
new Color(null, 215, 240, 255));
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE,
selectionAnchorStyle,
DisplayMode.SELECT,
SelectionStyleLabels.SELECTION_ANCHOR_STYLE);
Please look at our documentation on this:
https://www.eclipse.org/nattable/documentation.php?page=styling
http://www.vogella.com/tutorials/NatTable/article.html
But to answer your question, your painter only paints a rectangle and no content. Therefore you don't see the content anymore.
|
|
|
Re: Background color in a cell [message #1771479 is a reply to message #1771451] |
Fri, 25 August 2017 12:30 |
Angel Fraile Messages: 10 Registered: June 2016 |
Junior Member |
|
|
Hi Dirk, thank you for your help.
I want to apply the style only in some cells. I tried your code with a little change using my column label, but I do not get the style in my table (with or without le columnLaber parameter):
IStyle selectionCellStyle = new Style();
selectionCellStyle.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR,
new Color(null, 215, 240, 255));
natTableConfReg.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE,
selectionCellStyle,
DisplayMode.SELECT, columnLabel);
// selection anchor
IStyle selectionAnchorStyle = new Style();
selectionAnchorStyle.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR,
new Color(null, 215, 240, 255));
natTableConfReg.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE,
selectionAnchorStyle,
DisplayMode.SELECT,
SelectionStyleLabels.SELECTION_ANCHOR_STYLE);
natTable.configure();
|
|
| |
Goto Forum:
Current Time: Wed Apr 24 22:44:45 GMT 2024
Powered by FUDForum. Page generated in 0.02614 seconds
|