Selection of Custom JTable cell renderer inconsistenent between LAFs

Sorry if that seems cryptic, but I don't know if I've run into a bug or not. I've recently switched from windows to linux and have noticed a problem with one of my programs. I have a "multiline" renderer that I made for my JTable. The renderer isn't terribly elegant, but when I'd select the row of the table, I made the multi-line renderer change its colors in response to the selection.

This color selection switching works fine under the Metal LAF and the Windows LAF. However, this no longer works in the default look and feel is for linux (GTK+?). My multiline renderer in the JTable won't change its foreground/background in response to selection, but will if I use the Metal LAF under linux.

Any idea what I'm doing wrong? Is this a bug? (I'm fairly certain I'm doing something silly, I just can't see it for the life of me).

Here's the code I use to update the selection:

class MultilineRendererextends JTextArea

implements TableCellRenderer{

public MultilineRenderer(){

super();

setWrapStyleWord(false);

setLineWrap(true);

setFont(Const.TABLE_FONT);

setOpaque(true);

}

public Component getTableCellRendererComponent(

JTable table, Object text,

boolean isSelected,boolean hasFocus,

int row,int column){

String data = (String)text;

setText(data);

if (isSelected){

setForeground(table.getSelectionForeground());;

setBackground(table.getSelectionBackground());

}else{

setForeground(table.getForeground());

setBackground(table.getBackground());

}

returnthis;

}

}

[2482 byte] By [mr.v.a] at [2007-11-27 3:23:03]
# 1

Some look and feels will ignore calls to setBackground and setForeground, and will instead decide their own colour.

To quote the API documentation:

"The background color affects each component differently and the parts of the component that are affected by the background color may differ between operating systems."

I would imagine that this is your problem. Annoying, I know!

clairec666a at 2007-7-12 8:25:49 > top of Java-index,Desktop,Core GUI APIs...
# 2
I see...shoot. Any way of circumventing this for the Linux Look and Feel? It's a pretty bad eyesore to have one column look unselected..
mr.v.a at 2007-7-12 8:25:49 > top of Java-index,Desktop,Core GUI APIs...
# 3

You could set a colored line border around the row instead of changing the background color. It would let you see that the row is selected.

then just replace your call to setForeGround()

with

setBorder(BorderFactory.createLineBorder(Color.MAGENTA));

to remove it:

setBorder(new EmptyBorder(1, 1, 1, 1));

gracklemanna at 2007-7-12 8:25:49 > top of Java-index,Desktop,Core GUI APIs...