File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ListCellRenderer

 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still the same...
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want a 'hover' effect?
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes this is it
Thanks!
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic