01: package example;
02:
03: import java.awt.*;
04: import javax.swing.*;
05: import javax.swing.event.*;
06:
07: /** Source code for Tutorial 2. */
08:
09: public class LabeledList extends JPanel {
10: private JList list;
11: private JLabel label;
12:
13: public LabeledList(String[] initialContents) {
14: setLayout(new BorderLayout());
15: list = new JList(initialContents);
16: add(list, BorderLayout.CENTER);
17: label = new JLabel("Selected: ");
18: add(label, BorderLayout.SOUTH);
19: // Update the label whenever the list selection changes
20: list.addListSelectionListener(new ListSelectionListener() {
21: public void valueChanged(ListSelectionEvent lse) {
22: label.setText("Selected: " + list.getSelectedValue());
23: }
24: });
25: }
26: }
|