File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable colored row

 
Juergen Sprindt
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks!
Does anybody know how to color rows?
I retrieved records from a database into a JTable, but it would be great if special rows could be painted in different colors.
How can I do that?
Thanks in advance...
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Juergen,
In order to do that you'll have to install a custom TableCellRender. The way I would proceed (you may come up with a better way) is create a class, let's call it ColorDBCell which extends JPanel or maybe JTextField. This class will now hold the text representation of each db field. Next I create a ColorDBCellRender which must implement TableCellRenderer which contains one method:

Now inside of this method, you set the background color to whatever you want for our ColorDBCell and there you have it. Remember to typecast the Object value back to ColorDBCell like this:

Finally you need to register the renderer with the JTable similar to this:

This tells the JTable to use our custom renderer for all instances of ColorDBCell.
Hope this helps
Michael Morris
SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic