FileDocCategorySizeDatePackage
SimpleList2.javaAPI DocExample1736Mon Nov 09 12:45:54 GMT 1998None

SimpleList2

public class SimpleList2 extends JPanel implements ActionListener, ListSelectionListener

Fields Summary
String[]
label
JList
list
Constructors Summary
public SimpleList2()


      
        setLayout(new BorderLayout());

        list = new JList(label);
        JButton button = new JButton("Print");
        JScrollPane pane = new JScrollPane(list);

        DefaultListSelectionModel m = new DefaultListSelectionModel();
        m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m.setLeadAnchorNotificationEnabled(false);
        list.setSelectionModel(m);

        list.addListSelectionListener(this);
        button.addActionListener(this);

        add(pane, BorderLayout.NORTH);
        add(button, BorderLayout.SOUTH);
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)


        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);
        }
    
public static voidmain(java.lang.String[] s)

         JFrame frame = new JFrame("List Example");
         frame.addWindowListener(new BasicWindowMonitor());
         frame.setContentPane(new SimpleList2());
         frame.pack();
         frame.setVisible(true);
    
public voidvalueChanged(javax.swing.event.ListSelectionEvent e)

         System.out.println(e.toString());