I hope someone can help me with this...I've done a lot of searching but haven't found anything that addresses it.
I have a JList of objects, and I want to be able to right-click one of them and have a menu come up of things that I can do to that object (edit, delete, etc.). I'm getting the menu to come up okay, but it doesn't select the object. Instead, I have to first select it by left-clicking, then right-click to bring up the menu.
I'm sure there's a simple way to do it, but I can't seem to find it or figure it out.
Never mind, I've got it now. I don't know how I overlooked the locationToIndex() method on JList when I went into the API the other day specifically looking for something like that, but somehow it slipped past me. Once I spotted it this morning, everything fell into place. In case anyone else is interested...
Thanks Ken. I too just wasted about 1/2 hour trying to do this. The BasicListUI definitely doesn't want to react to right-click as in it's MouseInputHandler.adjustFocusAndSelection(mouseEvent) it has:
if (!SwingUtilities.isLeftMouseButton(e)) { return; }
Remember that mouseEvent has a getPoint() method, so you code could be even shorter...