This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable: Howto: Row color depending on value  Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "JTable: Howto: Row color depending on value  " Watch "JTable: Howto: Row color depending on value  " New topic
Author

JTable: Howto: Row color depending on value

Michael Boeni
Greenhorn

Joined: Oct 13, 2004
Posts: 5
Hi all

I would like to write a cell renderer that changes the background color of a row (or indivicual cells) depending on the value in one of the table columns. For example, if the value in column 'recursive' of a given row is 'true' then make the row (cell) backgroundd grey. if 'false' make white.

I ve been able to write a simple cell renderer:





But i am lost on how to actually check the value of the other column so I can determine which color to choose.

Help is greately appreciated.

Cheers,
michael
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8290

Take a look at the renderer examples in the Java Tutorial. The first example is similar to what you have. The second example is closer to what you want. Study the getTableCellRendererComponent method. It gives you a JTable reference which you can use to get the model so you can check your other columns, it gives you a row and a column so you know what cell you are rendering, and it returns the Component which will show up on the screen which you can color at will.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Michael Boeni
Greenhorn

Joined: Oct 13, 2004
Posts: 5
Thanks for the hint. I have now the following code:


My current problem is that I don't know what class to extend? I want to set the cell background, how do i do this? The rest seems to work ok.

BTW, I don't have to get the model, I can extract the values from the table directly. Is there any disadvantage to this approach?

Cheers,
M.
Michael Boeni
Greenhorn

Joined: Oct 13, 2004
Posts: 5
Has evolved a bit more:

Hi

here is the current status:

I can now set the background color, thanks for pointing it out (DefaultTableCellRenderer did the trick)

Two more problems:
- I noticed that I need to compare to one more value which can be got from a method of the class that creates the table. I am not really sure on how to get a reference to it to be able to retrieve this value.

Example:
- i have a class 'connect' (main gui class) that defines the jtable we are talking about. In my renderer, I now need to be able to call a method from the 'connect' class in order to correctly determine row color..what is the best way to do this?

second: The cell i use the above renderer on does not show selections anymore (the color does not change...what is the problem here?
[ October 13, 2004: Message edited by: Michael Boeni ]
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
There are two main ways I would think of approaching this :

1: Pass a reference to Connect in the constructor of your renderer.
2. Pass the information you need, via the getValue method of your table model.

HTH.
D.
Michael Boeni
Greenhorn

Joined: Oct 13, 2004
Posts: 5
Thanks to all, most issues have been resolved now. The only thing that remains is that the cells that use my renderer do not change colors when selected anymore. Why could that be?

The current code is here:
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
at the end of your method try

if (selected) {
setBackground(table.getSelectionBackground());
}
Michael Boeni
Greenhorn

Joined: Oct 13, 2004
Posts: 5
Thanks, Don. That did the trick...

Cheers,
michael
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: JTable: Howto: Row color depending on value
 
Similar Threads
must click 2 times after clearSelection()
JTable Cell Renderer
Please Help! JTable and TableCellRenderer color problem
Change the color of a JTable cell on mouse click
JTable renderer - Change the backColor of 2 specific rows.