FileDocCategorySizeDatePackage
ListModelExample.javaAPI DocExample1809Mon Nov 09 13:17:56 GMT 1998None

ListModelExample

public class ListModelExample extends JPanel implements ActionListener

Fields Summary
JList
list
DefaultListModel
model
int
counter
Constructors Summary
public ListModelExample()


      
        super(true);
        setLayout(new BorderLayout());
        model = new DefaultListModel();
        list = new JList(model);
        JScrollPane pane = new JScrollPane(list);
        JButton addButton = new JButton("Add Element");
        JButton removeButton = new JButton("Remove Element");
        for (int i = 0; i < 15; i++)
            model.addElement("Element " + i);

        addButton.addActionListener(this);
        removeButton.addActionListener(this);

        add(pane, BorderLayout.NORTH);
        add(addButton, BorderLayout.WEST);
        add(removeButton, BorderLayout.EAST);
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        if (e.getActionCommand() == "Add Element") {
            model.addElement("Element " + counter);
            counter++;
        } else {
            if (model.getSize() > 0)
                model.removeElementAt(0);
        }
    
public static voidmain(java.lang.String[] s)

         JFrame frame = new JFrame("List Model Example");
         frame.addWindowListener(new BasicWindowMonitor());
         frame.setContentPane(new ListModelExample());
         frame.setSize(300, 180);
         frame.setVisible(true);