JList: Double-click deselection without sensitizing entire viewport?

Discussion in 'Java' started by Charles Packer, Nov 5, 2003.

  1. Our customer wants its users to click _twice_ on a list item to remove it
    from a listbox. I figured out how to do this -- against all the odds, since
    I'm relatively new to Java. I have included example code below. The problem
    now is that the white space below the last list item is sensitive to
    mouse clicks and clicking _anywhere below_ the last item would remove the
    last item.

    The following example puts up a small window containing a list of four
    text strings. Clicking twice on a string causes a typeout to the terminal
    window of the index of the item clicked on. Note that double-clicking
    anywhere below the last item causes the index of the last item to be typed.
    I need to know how to de-sensitize that area.

    //-------------------- code follows
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    //-----------------------------------------
    public
    class
    aa extends JFrame {
    //-----------------------------------------
    public
    static
    void
    main(String[] args) {
    aa Fr = new aa(50, 50, 200, 200);
    Fr.show();
    }
    //-----------------------------------------

    public
    aa (int InitX, int InitY, int Width, int Height) {
    Create();
    setSize(new Dimension(Width, Height));
    setLocation(InitX, InitY);
    }
    //-----------------------------------------
    Vector Contents = new Vector();
    JList L = new JList();
    JPanel P = new JPanel();
    //-----------------------------------------
    private
    void
    Create () {
    new JFrame();
    Contents.addElement("Line0");
    Contents.addElement("Line1");
    Contents.addElement("Line2");
    Contents.addElement("Line3");
    L.setListData(Contents);
    L.setVisibleRowCount(10);
    L.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    L.addListSelectionListener(new ClickedOnTheViewport());
    L.addMouseListener(new ClickedTwiceOnIt());
    JScrollPane SP = new JScrollPane(L);
    P.setLayout(new BoxLayout(P, BoxLayout.Y_AXIS));
    P.add(SP);
    getContentPane().add(P);
    }
    //-----------------------------------------
    class
    ClickedOnTheViewport implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent e) {

    //OperateOnThisItem();
    }
    } // End of inner class definition

    //-----------------------------------------
    class
    ClickedTwiceOnIt extends MouseAdapter {
    public void mouseClicked (MouseEvent E) {
    if (E.getClickCount() >= 2)
    OperateOnThisItem();
    }
    } // End of inner class definition

    //-----------------------------------------
    void
    OperateOnThisItem () {
    System.out.println(L.getSelectedIndex());
    }
    //-----------------------------------------
    } // End of class definition
    Charles Packer, Nov 5, 2003
    #1
    1. Advertising

  2. Charles Packer

    Jeff Guest

    You simply need to check that the x, y location of
    the mouse click is within the bounds of the list item
    returned by getSelectedIndex() before removing it.
    Use JList.locationToIndex(Point location) to verify.
    --
    Jeff
    jlar310 at yahoo

    "Charles Packer" <> wrote in message news:...
    > Our customer wants its users to click _twice_ on a list item to remove it
    > from a listbox. I figured out how to do this -- against all the odds, since
    > I'm relatively new to Java. I have included example code below. The problem
    > now is that the white space below the last list item is sensitive to
    > mouse clicks and clicking _anywhere below_ the last item would remove the
    > last item.
    >
    > The following example puts up a small window containing a list of four
    > text strings. Clicking twice on a string causes a typeout to the terminal
    > window of the index of the item clicked on. Note that double-clicking
    > anywhere below the last item causes the index of the last item to be typed.
    > I need to know how to de-sensitize that area.
    >
    > //-------------------- code follows
    > import java.util.*;
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    > import javax.swing.event.*;
    > //-----------------------------------------
    > public
    > class
    > aa extends JFrame {
    > //-----------------------------------------
    > public
    > static
    > void
    > main(String[] args) {
    > aa Fr = new aa(50, 50, 200, 200);
    > Fr.show();
    > }
    > //-----------------------------------------
    >
    > public
    > aa (int InitX, int InitY, int Width, int Height) {
    > Create();
    > setSize(new Dimension(Width, Height));
    > setLocation(InitX, InitY);
    > }
    > //-----------------------------------------
    > Vector Contents = new Vector();
    > JList L = new JList();
    > JPanel P = new JPanel();
    > //-----------------------------------------
    > private
    > void
    > Create () {
    > new JFrame();
    > Contents.addElement("Line0");
    > Contents.addElement("Line1");
    > Contents.addElement("Line2");
    > Contents.addElement("Line3");
    > L.setListData(Contents);
    > L.setVisibleRowCount(10);
    > L.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    > L.addListSelectionListener(new ClickedOnTheViewport());
    > L.addMouseListener(new ClickedTwiceOnIt());
    > JScrollPane SP = new JScrollPane(L);
    > P.setLayout(new BoxLayout(P, BoxLayout.Y_AXIS));
    > P.add(SP);
    > getContentPane().add(P);
    > }
    > //-----------------------------------------
    > class
    > ClickedOnTheViewport implements ListSelectionListener {
    > public void valueChanged(ListSelectionEvent e) {
    >
    > //OperateOnThisItem();
    > }
    > } // End of inner class definition
    >
    > //-----------------------------------------
    > class
    > ClickedTwiceOnIt extends MouseAdapter {
    > public void mouseClicked (MouseEvent E) {
    > if (E.getClickCount() >= 2)
    > OperateOnThisItem();
    > }
    > } // End of inner class definition
    >
    > //-----------------------------------------
    > void
    > OperateOnThisItem () {
    > System.out.println(L.getSelectedIndex());
    > }
    > //-----------------------------------------
    > } // End of class definition
    Jeff, Nov 5, 2003
    #2
    1. Advertising

  3. Charles Packer

    Sudsy Guest

    Charles Packer wrote:
    > Our customer wants its users to click _twice_ on a list item to remove it
    > from a listbox. I figured out how to do this -- against all the odds, since
    > I'm relatively new to Java. I have included example code below. The problem
    > now is that the white space below the last list item is sensitive to
    > mouse clicks and clicking _anywhere below_ the last item would remove the
    > last item.
    >
    > The following example puts up a small window containing a list of four
    > text strings. Clicking twice on a string causes a typeout to the terminal
    > window of the index of the item clicked on. Note that double-clicking
    > anywhere below the last item causes the index of the last item to be typed.
    > I need to know how to de-sensitize that area.


    Javadocs are your friends! Sun provides an excellent example of exactly
    what you want to do. Here's the modified code:

    //-----------------------------------------
    class
    ClickedTwiceOnIt extends MouseAdapter {
    public void mouseClicked (MouseEvent E) {
    if (E.getClickCount() == 2) {
    int index = L.locationToIndex(E.getPoint());
    OperateOnThisItem( index );
    }
    }
    } // End of inner class definition

    //-----------------------------------------
    void
    OperateOnThisItem ( int index ) {
    System.out.println( "Index = " + index );
    }
    //-----------------------------------------
    } // End of class definition

    Note the invocation of locationToIndex with an argument of the
    mouse click location provided in the MouseEvent. If you click
    in the white area below the items then the index will be -1.
    Just what the doctor ordered, eh?
    Sudsy, Nov 5, 2003
    #3
    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. Sydex
    Replies:
    12
    Views:
    6,453
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    0
    Views:
    364
  3. oziris
    Replies:
    0
    Views:
    343
    oziris
    May 9, 2007
  4. Stefan Brückner

    Vertical viewport without scrollbars?

    Stefan Brückner, Apr 25, 2007, in forum: Javascript
    Replies:
    1
    Views:
    84
    Stefan Brückner
    Apr 25, 2007
  5. Me
    Replies:
    1
    Views:
    113
    scripts.contact
    Jul 6, 2007
Loading...

Share This Page