I want to code two JList (categories and items). When I click one category it should select all the items for that category and when I click on one item it should select its categories. So both JList will have a ListSelectionListener listening at each other and changing the selection.
Should I fear about some a kind of “loop” ? Is there a way to tell that an Event has been consumed ? how do people manage that kind of situation ?
Thanks
As you have imagined, each time make selection on
listA, you will trigger aListSelectionEventto be fired on your listener forlistA, whose job it is to find all appropriate items inlistBto select. Forcing selection then onlistBwill trigger events to be handled by yourlistBlistener. This will in turn force selection onlistA. 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
getSourcemethod on theListSelectionEvent. If the source islistB, remove your listener from the listenerlist oflistA, force selection onlistAand then readd.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.