Java Reference
In-Depth Information
Vector listData = new Vector();
for(int i = 0; i < model.getSize(); i++)
{
listData.addElement(model.getElementAt(i));
}
listData.removeElement(item);
source.setListData(listData);
}
private void addToDestination(String item)
{
ListModel model = destination.getModel();
Vector listData = new Vector();
for(int i = 0; i < model.getSize(); i++)
{
listData.addElement(model.getElementAt(i));
}
listData.addElement(item);
destination.setListData(listData);
}
}
In the SelectionHandler class, items are added to and removed from the
two JList components. There are no add() or remove() methods in the JList
class. To change the items in each JList, I used the getModel() method to
retrieve the elements as a ListModel object. Then, I iterated through the
ListModel, adding each element to a Vector. (See the removeFromSource()
and addToDestination() methods.) I made changes to the Vector and then
set the Vector as the new elements in the JList. It seems kind of tedious,
but I haven't seen a better way to do it.
Figure 13.13
Output of the SelectionDialog program.
Search WWH ::




Custom Search