Article View: borland.public.jbuilder.java.swing
Article #8706Re: Colored Rows in JTable.
From: John P. McGrath
Date: Sat, 22 Jul 2000 00:00
Date: Sat, 22 Jul 2000 00:00
40 lines
2009 bytes
2009 bytes
On Fri, 21 Jul 2000 10:45:04 -0500, "Jerel Thompson" wrote:
> I would like to have a JTable in my aplett that has
> multi-colored rows. I would like every other tow to
> be a light green as to make it easy to read. However,
> this being my first java applett, I am a little lost
> on where to start. I know it has something to do
> with a renderer but I have no idea where or what to
> code. All my data is in the JTable, I just need to
> color the cells. Any examples or snippits of code
> would be most helpful.
Yes, this can be done with a TableCellRenderer. Something like the
following ought to work. I have not tried it, so you may have to fix
some compile errors and perhaps do some debugging.
Try adding the following code to your JTable class. It should set the
background color to 'highlightColor' for every other row, starting with
row 0. But it does do so if the cell is selected, since the background
color is usually used to indicate the cell selection.
private Color highlightColor = new Color(r,g,b); // Fill in r,g,b
public Component prepareRenderer( TableCellRenderer renderer,
int row, int column )
{
Component c = super.prepareRenderer( renderer, row, column );
if ( ((row % 2) == 0) && ( !isCellSelected(row, column) ) ) {
c.setBackground( highlightColor );
}
return c;
}
I hope this works for you. Please post a message after you try it, and
let me know if it worked. Thanks & Good luck!
--
Regards,
John McGrath
Technical Details
Message-ID:
<MPG.13e2f3229a275165989a5d@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<39786dcb$1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,bdd9f7380b70666 X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: Colored Rows in JTable. Date: 2000/07/22 Message-ID: <MPG.13e2f3229a275165989a5d@forums.borland.com> X-Deja-AN: 649392036 References: <39786dcb$1@dnews> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing