As you have imagined, each time make selection on listA
, you will trigger a ListSelectionEvent
to be fired on your listener for listA
, whose job it is to find all appropriate items in listB
to select. Forcing selection then on listB
will trigger events to be handled by your listB
listener. This will in turn force selection on listA
. Simply using two listeners doesnt solve the problem.
I see two options:
1 - use a single listener. This listener will need to test the source of the event by using the getSource
method on the ListSelectionEvent
. If the source is listB
, remove your listener from the listenerlist of listA
, force selection on listA
and then readd.
list1.removeListSelectionListener(this);
list1.setSelectedIndex(e.getFirstIndex()); //this would have to be played with to allow for intervals
list1.addListSelectionListener(this);`
2 - use two listeners, however, to avoid the loop, you would need to test whether the item is already selected before attempting to select it. If it is already selected, dont reselect it.