addMouseListener to JList *ITEM*

Discussion in 'Java' started by Jack Crow, Sep 13, 2003.

  1. Jack Crow

    Jack Crow Guest

    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.
     
    Jack Crow, Sep 13, 2003
    #1
    1. Advertising

  2. Jack Crow wrote:
    > 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.
     
    =?ISO-8859-1?Q?Andree_Gro=DFe?=, Sep 15, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    477
  2. Replies:
    2
    Views:
    405
    Andrew Thompson
    Jan 9, 2004
  3. a

    JList item rename

    a, Nov 27, 2005, in forum: Java
    Replies:
    3
    Views:
    7,465
  4. Ce

    addMouseListener

    Ce, Jul 26, 2006, in forum: Java
    Replies:
    7
    Views:
    15,499
    Vincent van Beveren
    Jul 27, 2006
  5. Jon-Paul Dobson
    Replies:
    1
    Views:
    118
    Jim Higson
    Sep 15, 2004
Loading...

Share This Page