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 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.