Java Reference
In-Depth Information
There are other methods, so check the documentation when working with
List components. The following code from the ListDemo program, available
on the Web site for this topic, demonstrates filling a List with the days of the
week.
items = new List();
items.add(“Sunday”);
items.add(“Monday”);
items.add(“Tuesday”);
items.add(“Wednesday”);
items.add(“Thursday”);
items.add(“Friday”);
items.add(“Saturday”);
The ListDemo program displays the selected list item as a Label in the win-
dow. You need to double-click the item in the list so that an ActionEvent
occurs, and the following ShowSelection listener class handles this event.
Study the ListDemo program and the ShowSelection class, and try to deter-
mine how the output of the program looks. The output is shown in Figure
13.11.
import java.awt.event.*;
public class ShowSelection implements ActionListener
{
private Label display;
public ShowSelection(Label d)
{
display = d;
}
public void actionPerformed(ActionEvent a)
{
Object source = a.getSource();
if(!(source instanceof List))
{
return;
}
List list = (List) source;
String selected = list.getSelectedItem();
display.setText(selected);
}
}
Search WWH ::




Custom Search