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 JList
s, then you also have their respective ListModel
s. 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
2010-01-22 15:13:01
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
2010-01-22 15:21:19
Right, you have to implement your own model in that case. Or derive from DefaultListModel and extend it appropriately.
Joey
2010-01-22 15:36:16
+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
2010-01-22 15:30:34