• Post Reply Bookmark Topic Watch Topic
  • New Topic

How To Color Row In JTable

 
kishukishor patil
Greenhorn
Posts: 23
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • 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 Java Oracle
  • Mark post as helpful
  • send pies
  • 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...


 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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 Java Oracle
  • Mark post as helpful
  • send pies
  • 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
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • 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".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!