문제

class MyListListener implements ListSelectionListener {

      public void valueChanged (ListSelectionEvent e) {
           JList source = (JList) e.getSource();
           // do something
      }
}

JList myList = new JList (myModel);  
myList.addListSelectionListener (new MyListListener());

I am doing something very simple. I have a JList. If an item on the list is selected, the handler is called. The problem is the handler is invoked twice when I go from one item to another. I can see the use if the first trigger passes on the original selected item, and the second trigger passes on the new item. But both times, the same new item is passed. What is the point of that ? Is there a way to prevent the handler from being called twice ?

도움이 되었습니까?

해결책

e.getValueIsAdjusting is what you are looking for. As stated in the javadoc of that method

Returns whether or not this is one in a series of multiple events, where changes are still being made

다른 팁

It's the normal way, you have to filter with getValueIsAdjusting() method

If you really want to prevent this, try this:

  public void valueChanged (ListSelectionEvent e) {
       JList source = (JList) e.getSource();
       if(!e.getValueIsAdjusting()){
           // do something
       }
  }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top