JTable change cell colors using TableCellRenderer
Asked Answered
B

2

2

I am using a JTable in my GUI application as a grid to represent positions for a game. I want the cells of the table that represent a certain position of an object to have a certain color, and on some actions, the object to move (i.e the color cell to move around in the Grid /JTable). I know that I can change cell colors by making a class that extends DefaultTableCellRenderer , is this the only way it can be done? or is there a simpler way to chage cell colors??Also Is JXTable better than JTable for such an application ?

EDIT: I didn't include the fact that I need certain cell colors to change dynamically, i.e with button clicks, keyboard clicks ...etc, is that still possible with any TableCellRenderer in case I am still using JTable ?

Thanks,

Buckjumper answered 26/4, 2011 at 20:36 Comment(0)
K
3

With JTable, DefaultTableCellRenderer is the best way to do it.

However, if you use JXTable, it'll be much easier to use a Highlighter and a custom Predicate. Generally, Predicates are used to color based on contents of the cell, but you could just as easily have it color based on row/column and some external value.

Look in http://www.jarvana.com/jarvana/view/org/swinglabs/swingx-core/1.6.2/swingx-core-1.6.2-javadoc.jar!/org/jdesktop/swingx/JXTable.html under Rendering and Highlighting, which is a striped table and pattern matching. You'd essentially do the same thing as the pattern highlighter, but with your own Predicate.

Kolosick answered 26/4, 2011 at 20:57 Comment(1)
How can do this dynamically though? Does it work the same way if I need to click a button that change a cell color, or make a keyboard click change cell colors as well? Thanks YouBuckjumper
S
4

As an alternative, consider using prepareRenderer(), as suggested by @mKorbel and shown in the article Table Row Rendering.

Schappe answered 27/4, 2011 at 0:8 Comment(2)
can I use prepareRenderer() on a button action to change a color of one cell or a set of cells?Buckjumper
Yes, if the state is a pure function of row & column. See also this answer.Schappe
K
3

With JTable, DefaultTableCellRenderer is the best way to do it.

However, if you use JXTable, it'll be much easier to use a Highlighter and a custom Predicate. Generally, Predicates are used to color based on contents of the cell, but you could just as easily have it color based on row/column and some external value.

Look in http://www.jarvana.com/jarvana/view/org/swinglabs/swingx-core/1.6.2/swingx-core-1.6.2-javadoc.jar!/org/jdesktop/swingx/JXTable.html under Rendering and Highlighting, which is a striped table and pattern matching. You'd essentially do the same thing as the pattern highlighter, but with your own Predicate.

Kolosick answered 26/4, 2011 at 20:57 Comment(1)
How can do this dynamically though? Does it work the same way if I need to click a button that change a cell color, or make a keyboard click change cell colors as well? Thanks YouBuckjumper

© 2022 - 2024 — McMap. All rights reserved.