*
The moose likes Swing / AWT / SWT and the fly likes Color Some particular rows in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Color Some particular rows in JTable" Watch "Color Some particular rows in JTable" New topic
Author

Color Some particular rows in JTable

Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Hi All need help

I have a table and i have to implement a search functionality in that table for that i have created search button and performed search operation. Now i want that all rows who are having search values must change their color please help me with that problem example is here------

Now Assume that i have searched vinod then the entire row of vinod must change its colour...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

In future, while posting code, please UseCodeTags I have added them for for you this time. As you can see, the tags make the code much more easier to read and understand.

Coming back to your question, what you need is renderers. Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Maneesh Godbole wrote:In future, while posting code, please UseCodeTags I have added them for for you this time. As you can see, the tags make the code much more easier to read and understand.

Coming back to your question, what you need is renderers. Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer


Thanks for your suggestion but i think exact answer in the code form will be more helpful....
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

If you are looking for ready made code, then unfortunately, the Ranch is not the place. We are NotACodeMill because we believe the best way to learn is to be guided along the way and not being spoon fed.
Why don't you give the code a shot, and try it out. If you still can't get it right, you can post your SSCCE and we will help you figure out what's wrong with that code.
Best of luck!
Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Maneesh Godbole wrote:If you are looking for ready made code, then unfortunately, the Ranch is not the place. We are NotACodeMill because we believe the best way to learn is to be guided along the way and not being spoon fed.
Why don't you give the code a shot, and try it out. If you still can't get it right, you can post your SSCCE and we will help you figure out what's wrong with that code.
Best of luck!


I am not looking for a ready made code. I just want a help in any form like explain(a little) the calling of required methods as till now i am familiar with one method " getTableCellRendererComponent(JTable,Object,boolean,row,column) " but i am not getting the way how to use it. I mean i have go through with many example where this method is used, but i am unable to understood its working....
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Rachit Vaish wrote:i am not getting the way how to use it. I mean i have go through with many example where this method is used, but i am unable to understood its working....

It's a method defined in the inteface TableCellRenderer, that receives 6 parameters (not 5 as you have listed). The method is expected to make use of those parameters to returna java.awt.component subclass that is to be used by the caller to paint that cell in the JTable.

How it works -- what it returns -- is governed by the code inside the method, usually in conjunction with instance and/or static fields of the implementing class.

A common way to create a custom TableCellRenderer is by extending DefaultTableCellRenderer, a JLabel subclass. In such implementations, it is common to override the method something like:To create a custom renderer that isn't a JLabel, you can either extend the class of interest or maintain an instance of it as a field, and implement TableCellRenderer.

You can gain some more knowledge by reading the source of DefaultTableCellRenderer, will you will find in the src.zip in your JDK installation folder.


luck, db
There are no new questions, but there may be new answers.
Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Darryl Burke wrote:
Rachit Vaish wrote:i am not getting the way how to use it. I mean i have go through with many example where this method is used, but i am unable to understood its working....

It's a method defined in the inteface TableCellRenderer, that receives 6 parameters (not 5 as you have listed). The method is expected to make use of those parameters to returna java.awt.component subclass that is to be used by the caller to paint that cell in the JTable.

How it works -- what it returns -- is governed by the code inside the method, usually in conjunction with instance and/or static fields of the implementing class.

A common way to create a custom TableCellRenderer is by extending DefaultTableCellRenderer, a JLabel subclass. In such implementations, it is common to override the method something like:To create a custom renderer that isn't a JLabel, you can either extend the class of interest or maintain an instance of it as a field, and implement TableCellRenderer.

You can gain some more knowledge by reading the source of DefaultTableCellRenderer, will you will find in the src.zip in your JDK installation folder.


Thank you sir it is quite helpful for me, but i am still not able to use this method with my class as i have mentioned in my example, as we cannot call this method then how can we use it....??
Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Rachit Vaish wrote:
Darryl Burke wrote:
Rachit Vaish wrote:i am not getting the way how to use it. I mean i have go through with many example where this method is used, but i am unable to understood its working....

It's a method defined in the inteface TableCellRenderer, that receives 6 parameters (not 5 as you have listed). The method is expected to make use of those parameters to returna java.awt.component subclass that is to be used by the caller to paint that cell in the JTable.

How it works -- what it returns -- is governed by the code inside the method, usually in conjunction with instance and/or static fields of the implementing class.

A common way to create a custom TableCellRenderer is by extending DefaultTableCellRenderer, a JLabel subclass. In such implementations, it is common to override the method something like:To create a custom renderer that isn't a JLabel, you can either extend the class of interest or maintain an instance of it as a field, and implement TableCellRenderer.

You can gain some more knowledge by reading the source of DefaultTableCellRenderer, will you will find in the src.zip in your JDK installation folder.


Thank you sir it is quite helpful for me, but i am still not able to use this method with my class as i have mentioned in my example, as we cannot call this method then how can we use it....
I have also seen use of prepareRenderer(TableCellRenderer,row,col) but i am not able to use it in my code.....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Color Some particular rows in JTable