• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shans sat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recommended reading:
The Java™ Tutorials: How to Use Tables: Using Custom Renderers
 
Shans sat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shans sat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your valuable suggestion. Now it's working fine.


Thanks and Regards,
Shans
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic