JLabel in JList is not clickable


We add mouseListener in JList and we can detect the event when we click some item. But the problem is, we want to invoke an event when user click some Label in an item. For example, in the following screenshot, if user click the red region(it's actually a JLabel) in an item, we want to invoke an event, so we can add some logic.

Is this function possible to implement? If Yes, please give us some clues.

Any feedback will be much appreciated.

Thanks in advance!



Comment actions Permalink

A list content is painted with a fake component (renderer), which is shared between items.

All events are handled by a list itself, which usually processes a click as a selection.

You should add a custom mouse listener to your list and process clicks as needed.

Comment actions Permalink

Hi Sergey,

Thanks for your quick reply!

Yes. We have already added a mouse listener to JList and when clicking an item we can receive the click event and get the selected item. But the challenge we are facing is that an item is composed of several JLabels, after rendered, they are not clickable anymore. 

We just want to invoke an event when a JLabel is clicked. For example, in my attached screenshot, if users click the Label surrounded by a red rectangle, we will copy the content of the selected item to pasteboard.



Comment actions Permalink

You can try request a renderer and initialize it with proper size, then relayout and search for underlying label...

But the simplest way is to create a vertical box with similar components and provide a selection functionality.

See javax.swing.Box#createVerticalBox   javax.swing.DefaultSingleSelectionModel



Please sign in to leave a comment.