wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes ListCellRenderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ListCellRenderer" Watch "ListCellRenderer" New topic
Author

ListCellRenderer

Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Hi,
I have a JList and if the cursor is in the cells that i want to change his background. My code:



Doesn't work... the cells has green background even if the cursor is in the cells.

p.s. Sorry for my English
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I'm guessing, but could try calling repaint() and revalidate(), or one or the other, after changing the background colour.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
Don't know. But it looks like something which would sit better on our GUIs forum, so I shall move it.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
still the same...
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Instead of implementing MouseListener here, how about giving your JList a MouseListener (prefer composition over inheritance). You probably need to repaint the JList rather than this cell renderer. Also, I would extend a MouseAdapter rather than a MouseListener - it's easier.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Darryl
I want to get the same effect what you have if you click start in the Windows and moving the cursor. IsSelected returns true if i click on the item but this is not that i want
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
This code is just short example. The finally code will be more complicated: besides the text i set icon ect.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

So you want a 'hover' effect?
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
yes this is it
Thanks!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Note that the code I posted has scope for improvement. For example, now that I've thought about it some more, I should pass the JList to the constructor of the outer renderer class and construct the handler and add the listeners in that constructor. In the get...Component method I would check that the passed-in JList is the same list, and if not then throw an exception.

Doc comments should mention that this renderer implementation can't be shared between lists.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ListCellRenderer