File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem with ListCellRenderer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Problem with ListCellRenderer" Watch "Problem with ListCellRenderer" New topic
Author

Problem with ListCellRenderer

Florin Constantinescu
Greenhorn

Joined: Oct 26, 2009
Posts: 10
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

Joined: Jul 26, 2007
Posts: 8430

This will help
http://www.jroller.com/santhosh/entry/jlist_with_checkboxes


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Florin Constantinescu
Greenhorn

Joined: Oct 26, 2009
Posts: 10


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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with ListCellRenderer
 
Similar Threads
How to select multiple values using JCheckbox
JCheckBox in a JList
Using a custom renderer for JComboBox
Using JCheckBox as CellRenderer in JList
Using JCheckBox as CellRenderer in JComboBox