• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2736
  • Last Modified:

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;
        }
    }
   
0
kerzner
Asked:
kerzner
  • 4
  • 3
  • 2
  • +1
1 Solution
 
ksivananthCommented:
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;
        }
0
 
kerznerAuthor Commented:
No, that does not help.

Simply, if the elements inherits from JLabel, then of course it is not selectable.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ksivananthCommented:
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;
        }

0
 
CEHJCommented:
What happens with your own code?
0
 
kerznerAuthor Commented:
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
0
 
kerznerAuthor Commented:
CEHJ,

my own code shows correct colors, but it does not allow selecting elements in the JList
0
 
kerznerAuthor Commented:
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 :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now