• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TableCellRenderer Problem

 
Dennis Putnam
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I don't understand TableCellRenderer and need some help. I have the following:

It is setting the foreground color that is my problem (background works fine). When the 2 strings are found equal I want to set the foreground color of the current cell (column 4) of the current row to red. However, as soon as that condition is met all the columns in all the rows (including those already rendered) become red. What am I doing wrong? TIA.
 
Tony Docherty
Bartender
Posts: 2988
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is you aren't restricting the code to a specific row.

I don't know what SearchFrame is or why you are calling SearchFrame.getInstance() and not doing anything with the instance it returns.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as you have an else block for the background, you need an else block for the foreground that sets it to the default color.

Remember that a renderer is just one component, that is reconfigured for painting each cell in turn. Also, since you are extending DefaultTableCellRenderer, super.get...Component(...) returns 'this' -- so you don't need to assign the return value to a Component comp; just invoke setBack/Foreground(...) on the current instance. Makes your code shorter and cleaner.
 
Dennis Putnam
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

Tony: When I set 'comp' am I not specifying the row and column? What is 'comp' if not the current cell?

Darryl: I am not clear on what you are saying (my fuzziness of understanding is showing). The 'else' for the background is to set the opposite color on that row. Are you saying that because this is an override, I need to set both the color I want and the default color depending on the 'if' block?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. 'comp' is the renderer itself. Not the 'current cell' (whatever that may mean). See the source of DefaultTableCellRenderer, and also read the API.

2. There is only one renderer. Not one for each cell, which would defeat the purpose of using a renderer. Once you set its foreground (or any other property) that foreground remains set until you set it to something else.

Since the renderer component is the DefaultTableCellRenderer, your code for setting the background can be reduced toAlso, code with consistent and conventional indents is so much easier to read: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- for hightlighting whole row to use prepareRenderer, easiest of ways

- If is JTable filtered or sorted then you have to synchronize view_to_model int modelRow = convertRowIndexToModel(row); similair method for ColumnReorderingAlloved


 
Dennis Putnam
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is becoming a little clearer but still a bit murky and the API confuses more than helps. If I understand correctly, 'comp' is the renderer and is invoked when rendering each cell. The 'row' and 'column' values tell me which row and column (cell) is it currently rendering. In addition 'value' is the value of the current cell which I need to determine the class myself to figure out what it is.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you seen the JTable tutorial? That will help you much more than the javadoc, the javadoc is just a reference.

You shouldn't usually need to test the class of a value passed to a renderer. JTable allows you to set different renderer for each column (which is usually all that is needed, assuming one column always contains the same type of data). Create one renderer for each class and set them to individual columns using TableColumn.setCellRenderer().

Using different renderers for different cells in the same column is also possible, if you override JTable's getCellRenderer(int row, int column) method. You could put the logic to select proper renderer here and in the renderers themselves deal just with the cell appearances.
 
Dennis Putnam
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic