File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JList & ListSelectionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList & ListSelectionListener" Watch "JList & ListSelectionListener" New topic

JList & ListSelectionListener

netharam ram
Ranch Hand

Joined: Aug 09, 2001
Posts: 202
I have a JList with the ListSelectionListener, which displays the list of image files in the specified dir. When the item in the list is selected the corresponding image is displayed in a JLabel. The problem is I have the code as:
<JList>.addListSelectionListener(new ListSelectionListener()
public void valueChanged(ListSelectionEvent sev)
System.out.println("Calling getImages() from list");
//System.out.println("Client: "+getClient());
I get the output as :
Calling getImages() from list
Calling getImages() from list
the value changed is executed twice. Pls. give me a soln.
Happy middling with java.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

valueChanged() is called for both the new value being selected and for the old value being unselected. You can use a test based off the getValueIsAdjusting() method of ListSelectionEvent to only get the event representing the selection of the new value.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
netharam ram
Ranch Hand

Joined: Aug 09, 2001
Posts: 202
Thank U Nate.
I agree. Here's the link:
subject: JList & ListSelectionListener
It's not a secret anymore!