Java Reference
In-Depth Information
File: its/Lists/ListDemoFrame.java
1. package its.Lists;
2.
3. import its.SimpleFrame.SimpleFrame;
4. import java.awt.BorderLayout;
5. import javax.swing.JList;
6. import javax.swing.JScrollPane;
7.
8. public class ListDemoFrame extends SimpleFrame {
9.
private String[] entries = {"Schleswig-Holstein", "Niedersachsen",
10.
"Hamburg", "Bremen", "Mecklenburg-Vorpommern", "Brandenburg",
11.
"Berlin", "Nordrhein-Westfalen","Hessen","Sachsen-Anhalt",
12.
"Rheinland-Pfalz","Thuringen","Sachsen","Saarland", "Bayern",
13.
"Baden-Wurttemberg"};
14.
15.
public ListDemoFrame() {
16.
JList provinces = new JList(entries);
17.
JScrollPane scrollPane = new JScrollPane(provinces);
18.
this .getContentPane().add(scrollPane,BorderLayout.CENTER);
19.
this .pack();
20.
}
21.
22.
public static void main(String[] args) {
23.
ListDemoFrame ldf = new ListDemoFrame();
24.
ldf.showIt("List demo");
25.
}
26. }
16.2.2
Specification of a GUI with dynamic lists
We now consider lists where entries are added or removed and the user can select
entries and trigger an action as a response to that. We want to design a GUI with
two lists side by side. Initially only the left one contains some items. The user
is allowed to select single entries in the left list. Whenever an item is selected in
the left list which is not in the right list, it is copied to the right list. If the item
selected in the left list is already in the right list, it is deleted from the latter. The
left list is not changed during this process; only the right list changes. We use two
components to implement the desired functions: list models to handle the data
and listeners, which react to selecting list entries. We introduce these components
in the following sections.
16.2.3
List models
When lists are dynamic, i.e. when list entries are added or deleted in the course of
the program, the explicit use of a model for the list entries is recommended. The
Search WWH ::




Custom Search