views:

104

answers:

2

I have two JList on a swing GUI. Now I want that when a user clicks on a button (say TransferButton) the selected elements from one JList is added from the first JList to the second JList and remove those selected elements from the first JList.

+2  A: 

You have two JLists, then you also have their respective ListModels. Depending on how you implemented them you can just remove the elements from one model and add them to the other. Note, though, that the ListModel interface doesn't care for more than element access by default, so you probably have to implement add and remove methods there by yourself.

Joey
At present, the list models of both the JList is DefaultListModel but I can change that if required. The DefaultListModel doesn't provide any method like getSelectedItem or getSelectedItems ...
Yatendra Goel
Right, you have to implement your own model in that case. Or derive from DefaultListModel and extend it appropriately.
Joey
+3  A: 

The model doesn't know about selection.

The JList provides several methods to get the selected item or selected index. Use those methods to get the items and add them to the other list's model.

cconway