views:

50

answers:

3

Hi all, 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 of "loop" ? Is there a way to tell that an Event has been consumed ? how do people manage that kind of situation ?

Thanks

A: 

Look at the Beans Binding API. Here's a tutorial for NetBeans.

JRL
A: 

Two Listeners is good way how to do that, don't worry. Just be sure you create listeners only ONCE, not in loop.

Xorty
+1  A: 

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.

akf