this.setLayout(new BorderLayout()); list = new JList(label); JScrollPane pane = new JScrollPane(list); JButton button = new JButton("Print"); button.addActionListener(this); add(pane, BorderLayout.NORTH); add(button, BorderLayout.SOUTH);
int selected[] = list.getSelectedIndices(); System.out.println("Selected Elements: "); for (int i=0; i < selected.length; i++) { String element = (String)list.getModel().getElementAt(selected[i]); System.out.println(" " + element); }
JFrame frame = new JFrame("Simple List Example"); frame.addWindowListener(new BasicWindowMonitor()); frame.setContentPane(new SimpleList()); frame.setSize(250, 180); frame.setVisible(true);