File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable - can different rows be set to different colors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - can different rows be set to different colors?" Watch "JTable - can different rows be set to different colors?" New topic
Author

JTable - can different rows be set to different colors?

Michael Bell
Greenhorn

Joined: Dec 05, 2005
Posts: 19
I wish to set the row text color in a JTable depending upon a value found in one of the colums. Can this be done please?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
By subclassing JTable to override prepareRenderer, you can adjust the
cell renderer and it rendering component any way you like. Here I
make individual cells with odd data darker, but you could just as
easily have you effect constant across a give row...


There is no emoticon for what I am feeling!
Michael Bell
Greenhorn

Joined: Dec 05, 2005
Posts: 19
Is subclassing the only way to accomplish this?
I am using NetBeans 5.0 and so need to add the JTable subclass to a palette and then drag it on to the GUI. Its ok onto the palette but after that .....
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It's the only way I know -- perhaps forum members have another solution.
As for GUIs, I build them the old-fashion way. Drag'n'drop?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
You can iterate through the TableColumns, and for each column set the TableCellRenderer you want to use.
It's been years since I last used custom renderers (Java 1.1/Swing 1.1.1 timeframe), but back then they slowed down graphics tremendously.


Ping & DNS - my free Android networking tools app
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Ulf Dittmer:
You can iterate through the TableColumns, and for each column set the TableCellRenderer you want to use.
It's been years since I last used custom renderers (Java 1.1/Swing 1.1.1 timeframe), but back then they slowed down graphics tremendously.


The trouble with custom TableCellRenderers in this example (getting foreground color to depend on the data in a row) is that you typically have different types of data in different columns (simple strings, numbers, dates, currency, etc...) so you'll need to define different custom TableCellRenderers for different types of data and that's too much work. Overriding that one JTable method cuts accross renderers and does what you need to do.
Michael Bell
Greenhorn

Joined: Dec 05, 2005
Posts: 19
The subclassing method worked fine thanks. Adding it to the palette in Netbeans 5.0 (a jolly fine free IDE) was the problem but all solved now thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable - can different rows be set to different colors?
 
Similar Threads
excel but in a jTable
JTable
JTable
JTable
Jtable