Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JList & ListSelectionListener

 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank U Nate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic