summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing3710.java
blob: b271396ea4bac8974dab69d20fd8aa65e3d2e11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Listing3710.java */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Listing3710
extends JFrame
implements ActionListener
{
  static final String[] DATA = {
    "Hund", "Katze", "Meerschweinchen", "Tiger", "Maus",
    "Fisch", "Leopard", "Schimpanse", "Kuh", "Pferd",
    "Reh", "Huhn", "Marder", "Adler", "Nilpferd"
  };

  private JList list;

  public Listing3710()
  {
    super("JList");
    addWindowListener(new WindowClosingAdapter(true));
    Container cp = getContentPane();
    //Liste
    list = new JList(DATA);
    list.setSelectionMode(
      ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
    );
    list.setSelectedIndex(2);
    cp.add(new JScrollPane(list), BorderLayout.CENTER);
    //Ausgabe-Button
    JButton button = new JButton("Ausgabe");
    button.addActionListener(this);
    cp.add(button, BorderLayout.SOUTH);
  }

  public void actionPerformed(ActionEvent event)
  {
    String cmd = event.getActionCommand();
    if (cmd.equals("Ausgabe")) {
      System.out.println("---");
      ListModel lm = list.getModel();
      int[] sel = list.getSelectedIndices();
      for (int i = 0; i < sel.length; ++i) {
        String value = (String)lm.getElementAt(sel[i]);
        System.out.println("  " + value);
      }
    }
  }

  public static void main(String[] args)
  {
    Listing3710 frame = new Listing3710();
    frame.setLocation(100, 100);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}