This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes How to create border or change color in a JTable cell? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to create border or change color in a JTable cell?" Watch "How to create border or change color in a JTable cell?" New topic
Author

How to create border or change color in a JTable cell?

Shans sat
Greenhorn

Joined: Jan 24, 2009
Posts: 12
Dear Friends,
I created table in my application using JTable. In this table most of the cells has JCheckBoxes (which has created by using Boolean.class). In this table some of JCheckBox cells alone to be looked differently either by changing background color or by created border to that cell.
Could anyone please tell me how to set the background color for a particular cell or create the border for a particular cell?


Thanks in advance,
Shanmugam
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Recommended reading:
The Java™ Tutorials: How to Use Tables: Using Custom Renderers


luck, db
There are no new questions, but there may be new answers.
Shans sat
Greenhorn

Joined: Jan 24, 2009
Posts: 12
Thanks for your reply. But it's not working as i expect. If i try to change the color of the particular cell then the entire coloum color got changed. If i try to create the border of th particular cell then the entire column has a border though i specified the row and column numbers.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Shans sat wrote:Thanks for your reply. But it's not working as i expect. If i try to change the color of the particular cell then the entire coloum color got changed. If i try to create the border of th particular cell then the entire column has a border though i specified the row and column numbers.


If you show us the code of your TableCellRenderer we could then comment on it.

Keep in mind that the same Renderer is used over and over, so not only do you
need to add a color/border when you want one, but you also have to make sure
it's gone when you don't.

bitguru blog
Shans sat
Greenhorn

Joined: Jan 24, 2009
Posts: 12




MyCellRenderer class:




When I execute this code, all cells in column0 is displayed in red color and I'm unable to check / uncheck the JCheckBoxes in the column0.

But I need to change the row1 - row5 rows in red color.

Could anyone please help me in this?


Thanks in advance.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Shans sat wrote:

MyCellRenderer class:

When I execute this code, all cells in column0 is displayed in red color and I'm unable to check / uncheck the JCheckBoxes in the column0.

But I need to change the row1 - row5 rows in red color.

Could anyone please help me in this?


Being able to check/uncheck has nothing to do with the renderer. That's up to the TableCellEditor and your table model's setValueAt/getValueAt methods. [If your table model returns Boolean.class for getColumnClass(0) it should just work. Otherwise you have to specify an editor, in a manner similar to how you specified a renderer.]

As for every row of column 0 being red, it's because you sometimes set the renderer's background to red, but you never set it non-red. Try adding an else clause to your if to set it white or something. [I mentioned this in the post above, no?]

While I'm here let me point out that (Boolean.valueOf(value.toString()).booleanValue()) seems unnecessarily complicated. If you know that your table model returns Boolean values for this column (which I would generally recommend) then you could just do (Boolean.TRUE.equals(value)).
Shans sat
Greenhorn

Joined: Jan 24, 2009
Posts: 12
Thank you very much for your valuable suggestion. Now it's working fine.


Thanks and Regards,
Shans
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create border or change color in a JTable cell?