jQuery in Action, 2nd edition*
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
Author

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");
getImages(getClient());
//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.
Netharam.
Nathan Pruett
Bartender

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.


-Nate
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JList & ListSelectionListener