Search code examples
javaswinguser-interfacetablecellrenderer

How can a fill the background of a Cell only half using TableCellRenderer?


I want to colour the same cell of a table using 2 colours. I still have to display some text in the cell also and I want to do it by keeping the current structure.

I've tried with labels, panels, but I can't get it to work properly. Also, the colours have to updated based on the status on the cell (update the colour of the first half while keeping the colour of the second one).


Solution

  • You can write your own extension of a Component / JPanel that knows how to display a text and knows how to paint the background in 2 colors. This would mean that you have to overwrite paint or paintComponent in your custom component.

    Then, the getTableCellRendererComponent would return instances of this custom component.