• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me!!!  RSS feed

 
chenw
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program includes these sentences as follows. *************************************
String list_string[]={"one","two"}
private JList list=new JList(list_string);
....
list.addListSelectionListener(this);
....
public void valueChanged(ListSelectionEvent e)
{
System.out.println("help me");
}
...
************************************************
When running this program,I click the items of the list and the sentence of "System.out.println("help me")" seems to be executed twice.why? How can I make it executed only once?Thanks a lot.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am sure you have this line:
public void valueChanged(ListSelectionEvent e) {

Now add these two line directly after, and I believe that will solve your problem:
if (e.getValueIsAdjusting())
return;


------------------
Happy Coding,
Gregg Bolinger
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chenw,

Please read the Officail JavaRanch Policy on User Names and re-register with a name that follows the format "First Name" + space + "Last Name".

Thanks!

-Nate
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!