Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Background color in a cell
Background color in a cell [message #1771448] Fri, 25 August 2017 07:10 Go to next message
Angel Fraile is currently offline Angel FraileFriend
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);
            }
        }
    }


index.php/fa/30509/0/

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 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
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 Go to previous messageGo to next message
Angel Fraile is currently offline Angel FraileFriend
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();
Re: Background color in a cell [message #1771481 is a reply to message #1771479] Fri, 25 August 2017 12:35 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
It gets overriden by the default configurations if you do it this way. You should create a configuration and register that to the NatTable or a layer below instead of registering it directly.

Please read the documentation or look at the various examples!

[Updated on: Fri, 25 August 2017 12:36]

Report message to a moderator

Previous Topic:Potential bug in SelectionModel.java
Next Topic:Combo box Editor is not hiding/closing sometimes
Goto Forum:
  


Current Time: Fri Apr 19 04:04:56 GMT 2024

Powered by FUDForum. Page generated in 0.01823 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top