开发者

Java Swing: Clear JList without triggering listener

My situation is as follows: I've got a JList which triggers a search (using a ListSelectionListener) whenever a selection is made in the list. I'm trying to reset the selection on the list, using list.clearSelection(). The problem with this is that using clearSelection() triggers the listener, which calls the search: not 开发者_JS百科good.

Is there a way of clearing the selection on the list without having the listener triggered? Could this be a hint that I'm not using the listener properly? In general though, can you change something in a Swing UI without triggering events associated with that element?

Thanks.


Read the section from the Swing tutorial on How to Write a List Selection Listener for a working demo.

Normally two events are generated, one for the deselection and one for the selection. You should only be doing your search when something is selected. You should also be able to check selected index which should be something other than -1 I believe.


Try following pattern when using clear() method to clear Jlist:

ListObjectName.addListSelectionListener(new ListSelectionListener() {

  public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()){
      if(ListObjectName.getSelectedIndex()>-1){
        //Your Code
      }
    }
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜