Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with ListCellRenderer

 
Florin Constantinescu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JList which must have as elements instances of JPanel. On every panel I put a JCkeckBox(or a JButton). The list's renderer is something like this:



, with ListItem:



The problem is that the checkbox it is displayed but unfortunately it can't be checked! If I use a JButton instead, this can't be selected.
How can I solve this?
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Florin Constantinescu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thank you, Maneesh.
But in this code, as in this one: http://www.coderanch.com/t/344173/Swing-AWT-SWT-JFace/java/Using-JCheckBox-as-CellRenderer-JList there is a big problem:

In the mouseClicked function it is used as the source of the event the list's cell, not the checkbox. So if you click the part of the cell which is situated outside the checkbox - the latter becomes selected, and this is not convenient for me. I render(in my application) a JPanel which has in one corner a little checkbox. I need the check box to be selected only when I click on it, not on another element which lies on the panel, let's say an image, etc.

I've tried to retain a Vector of checkboxes(the very ones which are rendered in the cells) and to add MouseListeners to each checkbox, but I can't "obtain" them in the handler, because they are subcomponents of the list. So this:



will print something like that(I've added also a MouseEvent to the list):

"It is the list: javax.swing.JList"

And instead of implementing in the following style(to identify the checkbox in the stack) JUST TO GET A SIMPLE CHECKBOX working properly: http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html, I'd rather kill myself.

And finally, I can't use a JTable(which has both a default editor and a renderer) instead of a JList, because of Layout reasons(I use the JList because I need its HORIZONTAL_WRAP facility).

Is there a simple solution to get this done?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic