• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

ListCellRenderer

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing, but could try calling repaint() and revalidate(), or one or the other, after changing the background colour.
 
Marshal
Posts: 80871
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still the same...
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is just short example. The finally code will be more complicated: besides the text i set icon ect.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you want a 'hover' effect?
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes this is it
Thanks!
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic