Java Reference
In-Depth Information
16.2.5
Implementation of the list GUI
The following program ListTransferFrame demonstrates the use of dynamic lists.
The view part consists of two lists. Both are embedded into scroll panes which are
then glued into a panel. The panel is glued centrally into a frame. The panel has
a1
2 grid layout so that the lists are side by side. We do not derive classes for
these lists, because we do not add any additional functionality to them.
The list on the left contains the names of the German provinces. As mentioned
in the specification this list does not change. Therefore, do we not explicitly define
a model for it. The selection mode is set such that only a single entry can be
selected. As the right list is dynamic, we define our own list model. Entries are
inserted into or deleted from the model. The display of the right list is automatically
updated as a response to this.
For the control part we use class TransferListener which implements the in-
terface ListSelectionListener .Itisimplemented as an internal class of the
frame so that it has direct access to fields of the frame. When the listener's
valueChanged -method is informed, we first make sure that we are not in the
middle of a sequence of events that change the selection. If that is not the case, we
find out which entry is selected in the left list. This is stored in the string variable
sel .Wethen check whether sel is already an entry in the right list and - if so -
delete it there. Otherwise sel is added as an entry to the right list.
The program is listed below. When using it, note the following. If an entry A is
selected on the left it is copied to the right list (assuming it was not already there).
If this entry A is clicked again the corresponding entry A is not removed from the
right list. One first has to select a different entry B on the left and select A again to
remove it from the right list. The reason is, as explained above, that the selection
has to change in order for valueChanged to be activated. Figure 16.2 shows the
result.
×
Figure 16.2 The result of ListTransferFrame
Search WWH ::




Custom Search