How to set a specific cells backcolor in JTable?

  • Follow


I have the following line:
  ((JLabel) table.getCellRenderer(iLine,
COL_STATIONGROUP)).setBackground(Color.lightGray);

but this set *all* cells in column COL_STATIONGROUP to lightGray...

Can anyone help me out on this one?

--
Dag.

May you always keep a foot under your keel.


0
Reply Dag 8/26/2003 7:32:22 AM

Thank you...

I see how i can manipulate each cell's attributes in that code,
but how do I attach the renderer to my table?

--
Dag.

....but i'm a little bit
"K.Koper" <klaas@secorp[NOSPAMPLEASE].nl> wrote in message
news:3f4b3335$0$49099$e4fe514c@news.xs4all.nl...
> Hi,
>
> You need to create a custom TableCellRenderer which renders the cell
> with the correct background color, afaik it's the only way to do what
> you want to do.
>
> i.e.
>
> public MyTableCellRenderer extends DefaultTableCellRenderer {
> public Component getTableCellRendererComponent(
> JTable table,
> Object value,
> boolean isSelected,
> boolean hasFocus,
> int row,
> int column) {
>
> Component cell =
> super.getTableCellRendererComponent(table,value,isSelected, hasFocus,
> row, column);
> if( column+1 % 2 == 0 && !isSelected ) {
> // give all even rows which aren't selected a  // custom
> background color
> cell.setBackground(Color.LIGHT_GRAY);
> }
>
> return cell;
> }
> }
>
> The code above is untested but it should give you a general idea as to
> what you should do.
>
> HTH,
>
> Klaas
>
> Dag Sunde wrote:
> > I have the following line:
> >   ((JLabel) table.getCellRenderer(iLine,
> > COL_STATIONGROUP)).setBackground(Color.lightGray);
> >
> > but this set *all* cells in column COL_STATIONGROUP to lightGray...
> >
> > Can anyone help me out on this one?
> >
> > --
> > Dag.
> >
> > May you always keep a foot under your keel.
> >
> >
>


0
Reply Dag 8/26/2003 10:51:19 AM


K.Koper <klaas@secorp[nospamplease].nl> wrote:
> 
> public MyTableCellRenderer extends DefaultTableCellRenderer {
        ^
     class

>        public Component getTableCellRendererComponent(
>                JTable table,
>                Object value,
>                boolean isSelected,
>                boolean hasFocus,
>                int row,
>                int column) {
>                
>                Component cell =         
> super.getTableCellRendererComponent(table,value,isSelected, hasFocus, 
> row, column);

It is know that "cell" == this.


>                if( column+1 % 2 == 0 && !isSelected ) {
                    column % 2 == 1


> //                      give all even rows which aren't selected a      //                      custom 
> background color                        

Wrapping Java code can make it illegal.


>                        cell.setBackground(Color.LIGHT_GRAY);
>                }



In subclasses of DefaultTableCellRenderer, either *always* call
setForeground/Background or *never*, but not conditionally.

http://www.chka.de/swing/table/faq.html

"Appearance / Renderers"


Christian
0
Reply usenet 8/26/2003 12:03:08 PM

I wrote the code in my mail client, so I expected it to contain errors 
.... lol

it was just an example for him, not something to be copy/pasted literally :)

anyway ... you're table faq is a nice read, thanks :)

0
Reply K 8/26/2003 12:21:54 PM

3 Replies
488 Views

(page loaded in 0.038 seconds)

Similiar Articles:













7/26/2012 10:38:59 AM


Reply: