addMouseListener to JList *ITEM*

J

Jack Crow

I have:

JList nameList;
DefaultListModel nameListModel; (with SINGLE_SELECTION)

I would like to add a listener to individual JList items (as they are
added to the JList, or after) so that I can bring up a popupmenu on
RightClick of an individual selected JList item (only when RightClick
is physically on the currently selected item "field"/row).

Perhaps the elements are considered part of the ListModel, but that
doesn't change the problem.

The elements are added using addElement(String newName); so how do I
attach a MouseListener to that? I am avoiding adding a MouseListener
to the JList then doing some math via counting the number of elements
and figuring out their individual bounds and then counting down...to
figure out if the RightClick is "within the bounds" of the "currently
selected item", because that seems a rather brutish solution.
 
?

=?ISO-8859-1?Q?Andree_Gro=DFe?=

Jack said:
I have:

JList nameList;
DefaultListModel nameListModel; (with SINGLE_SELECTION)

I would like to add a listener to individual JList items (as they are
added to the JList, or after) so that I can bring up a popupmenu on
RightClick of an individual selected JList item (only when RightClick
is physically on the currently selected item "field"/row).

You can not add a special listener to single standard JList-Items.
The parameter of JList.add() is type of Object.

I see 2 ways to do that.
1.Exchange the Popmenu within the ListSelectionListener.
2.Create your own JList-Item-Class and get the Popup
from selected item after calculation the list index
from point of MouseEvent.
(use selectedIndex = list.locationToIndex(theEvent.getPoint()))

A.G.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top