Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable: Howto: Row color depending on value

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michael Boeni
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at the end of your method try

if (selected) {
setBackground(table.getSelectionBackground());
}
 
Michael Boeni
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Don. That did the trick...

Cheers,
michael
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic