• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic