🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8713

Re: Colored Rows in JTable.

#8713
From: Andreas Schnell
Date: Mon, 24 Jul 2000 00:00
43 lines
1891 bytes
"Jerel Thompson" <jerel[at]rli-net[dot]net> schrieb im Newsbeitrag
news:39786dcb$1@dnews...
> 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.
Thanks
> for your time.
>

Hi,

you have to implement your own TableCellRender like this one, this works
very fine.

  private class MyTableCellRenderer implements TableCellRenderer
  {
     public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column)
     {
          JLabel theLabel = new JLabel();

          theLabel.setText(value.toString());

          theLabel.setOpaque(true);

          theLabel.setBackground(row%2 == 1 ? Color.green : Color.white);
          return theLabel;
     }
 }

Set the DefaultCellRenderer in your JTable class for all classes you
displays in the Table. like this one..

  setDefaultRenderer(Object.class, new MyTableCellRenderer());


Hope this helps

Andreas Schnell
Technical Details
Message-ID: <397c1137$1_1@dnews>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <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: "Andreas Schnell" <Andreas.Schnell@innovations.de>
Subject: Re: Colored Rows in JTable.
Date: 2000/07/24
Message-ID: <397c1137$1_1@dnews>
X-Deja-AN: 650061206
References: <39786dcb$1@dnews>
X-Trace: 24 Jul 2000 02:49:43 -0800, 194.123.233.30
X-MSMail-Priority: Normal
X-Priority: 3
Newsgroups: borland.public.jbuilder.java.swing
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600