aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Why doesn't a component in a Listbox see mouse clicks when using a DefaultListCellRenderer ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't a component in a Listbox see mouse clicks when using a DefaultListCellRenderer ?

Jim McGlaughlin
Greenhorn

Joined: Apr 13, 2009
Posts: 4
I have a listbox which contains a custom component consisting of a panel which in turn contains other panels.

The listbox has a DefaultListCellRenderer which tells me when the component has focus, is selected etc.

But, the mouse clicks on the component (my Panel(s)) are not 'seen' by the component.

My renderer only implents getListCellRendererComponent.

Should I be implementing some other methods in the renderer or ???

Thanks
Jim


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
A renderer is NOT a real component. It only paints an image of a component, so it does not receive events.

If you have multiple nested panels it sounds like you should be using a panel with a BoxLayout and just use real panels.

Or if you have multiple components, maybe you should be using a JTable with individual columns for each piece of data.
Jim McGlaughlin
Greenhorn

Joined: Apr 13, 2009
Posts: 4
The 'panels' I am putting in the listbox start with a panel which does use the BoxLayout.

If I don't use the renderer the panels will not display, just Strings that describe the panels.

A JTable would be a step back from what I was hoping to have as a user interface.

Do you know if I implement a mouse listener, if it would be practical to pass the click points to the underlying panel ? I don't think so.

I'm going to have to re-think my user interface.

Thanks
Jim
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

You can do it the hard way as well:
1) use the API to get the row on which the user clicked
2) use the cell renderer to get the renderer component for that cell
3) translate the mouse point such that it is relative to the renderer component
4) handle the mouse click manually given the translated mouse point

I've done this for a table and a tree renderer in the past, and it works.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why doesn't a component in a Listbox see mouse clicks when using a DefaultListCellRenderer ?