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.
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.
There are no new questions, but there may be new answers.
Joined: Feb 03, 2012
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?
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.
- 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
Joined: Feb 03, 2012
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.
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.