Notice that one of the parameters to the
getListCellRendererComponent(...) method is "
boolean isSelected' ... then think what you can do with that.
edit:It's usually more convenient to extend DefaultListCellRenderer than to implement ListCellRenderer. But for merely changing background/selection background colors, you don't need to extend or implement anything -- just use the methods available in the JList API. Moreover, renderers aren't added to any component hierarchy so can't respond to input events. And in any case, implementing MouseListener in a lass extending JLabel doesn't automagically add the class as its own MouseListener, without code that explicitly does just that.
Finally, do you really want
all the cells in the JList to display the
word "test"?
@Luigi: No. Those method calls are relevant when adding/removing components to/from an already visible container.