Link to home
Start Free TrialLog in
Avatar of kerzner
kerznerFlag for United States of America

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 getListCellRendererComponent( 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;
        }
    }
   
Avatar of ksivananth
ksivananth
Flag of United States of America image

try this,

public Component getListCellRendererComponent( 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);
            }
          }
            return this;
        }
Avatar of kerzner

ASKER

No, that does not help.

Simply, if the elements inherits from JLabel, then of course it is not selectable.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this

public Component getListCellRendererComponent( 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.getSelectionBackground());
                setForeground(list.getSelectionForeground());
          }
            return this;
        }

What happens with your own code?
Avatar of kerzner

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
Avatar of kerzner

ASKER

CEHJ,

my own code shows correct colors, but it does not allow selecting elements in the JList
Avatar of kerzner

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 :)