kerzner
asked on
Show JList elements in different colors
Hi experts,
I need to show elements in JList in different colors. I have found an example on the web using labels (see below), but if I do this, I can not select elements in JList anymore. Is there anything simpler and better?
Thank you.
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererCompone nt( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
Tree child = (Tree) value;
setBackground(Color.white) ;
if (child instanceof ContactVO) {
setForeground(Color.black) ;
} else {
setForeground(Color.blue);
}
return this;
}
}
I need to show elements in JList in different colors. I have found an example on the web using labels (see below), but if I do this, I can not select elements in JList anymore. Is there anything simpler and better?
Thank you.
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererCompone
setText(value.toString());
Tree child = (Tree) value;
setBackground(Color.white)
if (child instanceof ContactVO) {
setForeground(Color.black)
} else {
setForeground(Color.blue);
}
return this;
}
}
ASKER
No, that does not help.
Simply, if the elements inherits from JLabel, then of course it is not selectable.
Simply, if the elements inherits from JLabel, then of course it is not selectable.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try this
public Component getListCellRendererCompone nt( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
Tree child = (Tree) value;
if( ! isSelected ){
setBackground(Color.white) ;
if (child instanceof ContactVO) {
setForeground(Color.black) ;
} else {
setForeground(Color.blue);
}
}else{
setBackground(list.getSele ctionBackg round());
setForeground(list.getSele ctionForeg round());
}
return this;
}
public Component getListCellRendererCompone
setText(value.toString());
Tree child = (Tree) value;
if( ! isSelected ){
setBackground(Color.white)
if (child instanceof ContactVO) {
setForeground(Color.black)
} else {
setForeground(Color.blue);
}
}else{
setBackground(list.getSele
setForeground(list.getSele
}
return this;
}
What happens with your own code?
ASKER
objects, a few classes are still missing
DefaultStyleFactory
StyledObject
I may just learn from your code rather than use it direct - it is becoming too much additional code. But who knows, maybe I will need all that flexibiility
DefaultStyleFactory
StyledObject
I may just learn from your code rather than use it direct - it is becoming too much additional code. But who knows, maybe I will need all that flexibiility
ASKER
CEHJ,
my own code shows correct colors, but it does not allow selecting elements in the JList
my own code shows correct colors, but it does not allow selecting elements in the JList
ASKER
a few more files on object's site I was able to find myself. Now it works. I guess, no less code than that will not work - but on the other hand, it makes for a pretty fancy interface :)
public Component getListCellRendererCompone
setText(value.toString());
Tree child = (Tree) value;
if( ! isSelected ){
setBackground(Color.white)
if (child instanceof ContactVO) {
setForeground(Color.black)
} else {
setForeground(Color.blue);
}
}
return this;
}