Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Jlist action question

Posted on 2002-05-28
4
Medium Priority
?
308 Views
Last Modified: 2012-06-27
I'm working on some code that uses jlist. It is working
fine except my action runs on mouse down and then again
on mouse up.  I just want one action for a single mouse
click (up and down).  Can anybody help?

Here is the code i'm using:

   listModel = new DefaultListModel();

    for (int i=0; i <= InData.size() -1; i++)
      listModel.addElement(InData.elementAt(i));

    list = new JList(listModel);
    list.setSelectionModeListSelectionModel.SINGLE_SELECTION);


list.addListSelectionListener(this);


  public void valueChanged(ListSelectionEvent e) {
       Object xx=list.getSelectedValue();
       jTextArea1.append(xx.toString()+ "\n");
  }
0
Comment
Question by:afsac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 total points
ID: 7040529
Try:

    public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting() == false) {
            Object xx=list.getSelectedValue();
            jTextArea1.append(xx.toString()+ "\n");
        }
    }

Best regards,
Jim Cakalic
0
 
LVL 92

Expert Comment

by:objects
ID: 7040627
Or use a MouseListener, and react to mouseClicked events.
0
 

Author Comment

by:afsac
ID: 7040692
Fantastic! Works like a charm.  I see from your code that
the solution is:

if (e.getValueIsAdjusting() == false) {
....
}

If you have time, could you give a brief explanation of
what this is, how in the world would I have ever found
this on my own, etc.

Thank you!

AFSAC
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7042175
Straight out of the Java Tutorial section on using JList:
    http://java.sun.com/docs/books/tutorial/uiswing/components/list.html

No matter which selection mode your list uses, the list fires list selection events whenever the selection changes. You can process these events by adding a list selection listener to the list with the addListSelectionListener method. A list selection listener must implement one method: valueChanged. Here's the valueChanged method for the listener in SplitPaneDemo:

public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting())
        return;

    JList theList = (JList)e.getSource();
    if (theList.isSelectionEmpty()) {
        picture.setIcon(null);
    } else {
        int index = theList.getSelectedIndex();
        ImageIcon newImage = new ImageIcon("images/" + (String)imageList.elementAt(index));
        picture.setIcon(newImage);
        picture.setPreferredSize(new Dimension(newImage.getIconWidth(), newImage.getIconHeight()));
        picture.revalidate();
    }
}

Many list selection events can be generated from a single user action such as a mouse click. The getValueIsAdjusting method returns true if the user is still manipulating the selection. This particular program is interested only in the final result of the user's action, so the valueChanged method updates the image only if getValueIsAdjusting returns false.

:-)
Jim
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question