How do I add a mouse listener to JList item?

T

Todd

Hello,

I have been beating my brains out trying to figure this out, so this
means that there must be some patently obvious way of doing this that
if it had teeth it would have bitten me by now for being so obtuse.
After Googling repeatedly and reading the Sun Tutorials, I turn to
you.

I am trying to add an isPopupTrigger (right-click on Windows) filtered
JPopupMenu to individual items in a JList. I have tried the following
unsuccessfully:
1. making the list items implement MouseInputListener (result: mouse
listener doesn't get activated)
2. adding a mouse listener via a ListCellRenderer which extends JLabel
(need to set icons for list items) (result: popup can occur anywhere
in the JList display area)

and several other methods that I can't recall at this time. My brain
is jelly at this point.

If you know how to do this, please point me in the right direction
while my sanity remains (oh so minimally) intact.

Thanks,
Todd
 
T

Todd

All,

I figured it out (list is my JList):

public void mouseReleased(final MouseEvent e) {
if (e.isPopupTrigger()) {
final JPopupMenu menu = // define your popup menu

// Get the position of the click
final int x = e.getX();
final int y = e.getY();

// Verify that the click occured on the selected cell
final int index = list.getSelectedIndex();
final Rectangle bounds = list.getCellBounds(index, index);
if (null != bounds && bounds.contains(x, y)) {
menu.show(e.getComponent(), x, y);
}
}
}

Todd
 
T

Todd

Sorry forgot to mention that the listener was added as follows:

list.addMouseListener(new MouseAdapter(){
public void mouseReleased(final MouseEvent e) {
if (e.isPopupTrigger()) {
final JPopupMenu menu = // define your popup menu

// Get the position of the click
final int x = e.getX();
final int y = e.getY();

// Verify that the click occured on the selected cell
final int index = list.getSelectedIndex();
final Rectangle bounds = list.getCellBounds(index, index);
if (null != bounds && bounds.contains(x, y)) {
menu.show(e.getComponent(), x, y);
}
}
}
});
 
K

Knute Johnson

Todd said:
Hello,

I have been beating my brains out trying to figure this out, so this
means that there must be some patently obvious way of doing this that
if it had teeth it would have bitten me by now for being so obtuse.
After Googling repeatedly and reading the Sun Tutorials, I turn to
you.

I am trying to add an isPopupTrigger (right-click on Windows) filtered
JPopupMenu to individual items in a JList. I have tried the following
unsuccessfully:
1. making the list items implement MouseInputListener (result: mouse
listener doesn't get activated)
2. adding a mouse listener via a ListCellRenderer which extends JLabel
(need to set icons for list items) (result: popup can occur anywhere
in the JList display area)

and several other methods that I can't recall at this time. My brain
is jelly at this point.

If you know how to do this, please point me in the right direction
while my sanity remains (oh so minimally) intact.

Thanks,
Todd

Add the MouseListener to the JList and use JList.pointToIndex() to get
the index of the list item that was clicked. Then open the menu you want.
 
K

Knute Johnson

Knute said:
Add the MouseListener to the JList and use JList.pointToIndex() to get
the index of the list item that was clicked. Then open the menu you want.

Sorry, JList.locationToIndex()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top