This week's book giveaway is in the Open Source forum.
We're giving away four copies of RabbitMQ in Depth and have Gavin Roy on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Coloring a cell in a table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Coloring a cell in a table" Watch "Coloring a cell in a table" New topic

Coloring a cell in a table

Ken Rubin
Ranch Hand

Joined: Mar 08, 2004
Posts: 66

I am trying to create a simple Sudoku game and want to change the color of a cell in a table to light gray. My TableCellRender is below. Does anyone have a way to do this?

Thank you,


class ElementRenderer extends JLabel implements TableCellRenderer {

public Component getTableCellRendererComponent(
JTable table, Object color,
boolean isSelected, boolean hasFocus,
int row, int column) {

//Find integer value in array, convert to string and make part of label
JLabel theElement = new JLabel(arrayMatrix[row][column].toString(),CENTER);

return theElement;
//return this;
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10722

I am not sure I understand what problem exactly you have run into.
Check out the following code snippet.

Points to note:
1) You need to explicitly set the renderer to the table. The line table.setDefaultRenderer(Object.class....) means use this renderer whenever you encounter the Object type of object
2) Setting the renderer for the object class would be the best bet for your Sudoku application. The object class would work for all the data types (String as in "5" Integer/int as in 5 and even if you switch to icons/images which display the number 5)
3) Since you are rendering as a label, you dont really need to subclass JLabel and implement the TableCellRenderer interface. The default rendered component of the DefaultTableCellRenderer is the label iteself. Also this label is opaque. So no need to explicitly call label.setOpaque(true);
4) You are constructing the label value from some array matrix from inside the renderer method. This is not really a good thing to do. Always provide a TableModel or Object[][]rowData to the table. You always get a reference to the cell object in the getTableCellRendererComponent() method if you want to use it.
5) Long time back I had developed a game for my daughter (it was noughts and crosses though) where I had a similar requirement of an interactive grid like UI. After playing around I realized that using a panel with a grid layout was the best approach. You might want to consider this approach as against the JTable unless of course your requirement is that it has to be a JTable.

Does all this solve your problem?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42956
Doesn't setBackground work if you apply it to theElement?
Ken Rubin
Ranch Hand

Joined: Mar 08, 2004
Posts: 66
Thank you to Ulf and Maneesh for helping me set the colors of the table cells.

I needed to set the background color of the JLabel and setOpaque(true) as well as you indicated.

I appreciated the overall design considerations and will make some other modifications.

Thanks again,

I agree. Here's the link:
subject: Coloring a cell in a table