aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes TableCellRenderer Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TableCellRenderer Problem" Watch "TableCellRenderer Problem" New topic
Author

TableCellRenderer Problem

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
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

Joined: Aug 07, 2007
Posts: 2366
    
  50
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

Joined: May 03, 2008
Posts: 4658
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
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

Joined: May 03, 2008
Posts: 4658
    
    5

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

Joined: Jun 19, 2012
Posts: 156
- 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

Joined: Feb 03, 2012
Posts: 217
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

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Feb 03, 2012
Posts: 217
Got it. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TableCellRenderer Problem