• 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:

How To Color Row In JTable

 
Greenhorn
Posts: 23
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have problem that I want to color a specific row without clicking it but coloring should be based on searching. Means I have large set of records in JTable and I want to search one record. But now I want to highlight that row.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't ever capitalize every single word in a post again. Ever. It makes it harder to read your post so I fixed it.

You must use a custom TableCellRenderer, that looks at the search value and uses a different background color if the value matches. In pseudo code:
Do a search around this forum for other examples of custom renderers.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to override the prepareRenderer() method to do row level highlighting so you don't have to create custom renderers for each class of data in the table.

In your case you might change the code to use a Set containing the Integers of the rows you want highlighted. When you are rendering a row contained in the set you change the background otherwise you do nothing.

A quick example to get you started:

 
kishukishor patil
Greenhorn
Posts: 23
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You...

Here i did all this the code is below but the problem still exist that no any row is colored. I don't know where to put the line

itemTable.setDefaultRenderer(Object.class,new RowColorRenderer());

Below is my code... please check it and let me know where is fault...


 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In future, while posting the code, please use code tags

kishukishor patil wrote:
Here i did all this the code is below but the problem still exist that no any row is colored. I don't know where to put the line
itemTable.setDefaultRenderer(Object.class,new RowColorRenderer());



Recommended reading http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender

 
kishukishor patil
Greenhorn
Posts: 23
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All... Thanks

My above code worked perfectly but for column coloring ... It colored columns not rows .. i need row coloring. what is the problem. how it will be done
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kishukishor patil wrote:Hi All... Thanks

My above code worked perfectly but for column coloring ... It colored columns not rows .. i need row coloring. what is the problem. how it will be done


if i understand your requirement you want to color the row that has a match with your keyword search

so, we have to:
1) populate the JTable
2) write the code that colors the row when a match is reached

this code has to be written inside some method; which? getTableCellRendererComponent()
where to find it? in DefaultTableCellRenderer class

so, all you have to do is extending DefaultTableCellRenderer and overrride its getTableCellRendererComponent()
the example i give bellow searches for the "Pierrot le fou" keyword

for further details have a look at this recent topic
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It colored columns not rows .. i need row coloring



And you've already been given a working example of how to do that? All you have to do is change the "if statement".
 
You got style baby! More than this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic