File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem with ListCellRenderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with ListCellRenderer" Watch "Problem with ListCellRenderer" New topic

Problem with ListCellRenderer

Florin Constantinescu

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: 10825

This will help

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

Joined: Oct 26, 2009
Posts: 10

Thank you, Maneesh.
But in this code, as in this one: 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:, 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:
subject: Problem with ListCellRenderer
jQuery in Action, 3rd edition