Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coloring a cell in a table

 
Ken Rubin
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't setBackground work if you apply it to theElement?
 
Ken Rubin
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic