aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Coloring a cell in a table" Watch "Coloring a cell in a table" New topic
Author

Coloring a cell in a table

Ken Rubin
Ranch Hand

Joined: Mar 08, 2004
Posts: 66
Hello,

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,

Ken

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);
setOpaque(true);
theElement.setFont(theFont);
setBackground(Color.BLACK);

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

Joined: Jul 26, 2007
Posts: 9990
    
    7

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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Doesn't setBackground work if you apply it to theElement?


Ping & DNS - updated with new look and Ping home screen widget
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,

Ken
 
wood burning stoves
 
subject: Coloring a cell in a table
 
Similar Threads
How to disable JTable cell/row selection?
Color in JTable
Change the color of a JTable cell on mouse click
JTable Cell Renderer
JTable renderer - Change the backColor of 2 specific rows.