Java DefaultListModel() Constructor

Syntax

DefaultListModel() constructor from DefaultListModel has the following syntax.

public DefaultListModel()

Example

In the following code shows how to use DefaultListModel.DefaultListModel() constructor.


import java.awt.BorderLayout;
//from w  ww  .  j  av  a2  s . co m
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

public class Main {
  static String labels[] = { "a", "b", "c", "d" };

  public static void main(String args[]) {
    JFrame frame = new JFrame("Modifying Model");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
      model.addElement(labels[i]);
    }
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.WEST);

    ListDataListener listDataListener = new ListDataListener() {
      public void contentsChanged(ListDataEvent listDataEvent) {
        appendEvent(listDataEvent);
      }
      public void intervalAdded(ListDataEvent listDataEvent) {
        appendEvent(listDataEvent);
      }
      public void intervalRemoved(ListDataEvent listDataEvent) {
        appendEvent(listDataEvent);
      }
      private void appendEvent(ListDataEvent listDataEvent) {
        switch (listDataEvent.getType()) {
        case ListDataEvent.CONTENTS_CHANGED:
          System.out.println("Type: Contents Changed");
          break;
        case ListDataEvent.INTERVAL_ADDED:
          System.out.println("Type: Interval Added");
          break;
        case ListDataEvent.INTERVAL_REMOVED:
          System.out.println("Type: Interval Removed");
          break;
        }
        System.out.println(", Index0: " + listDataEvent.getIndex0());
        System.out.println(", Index1: " + listDataEvent.getIndex1());
        DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource();
        System.out.println(theModel);
      }
    };

    model.addListDataListener(listDataListener);

    model.add(0, "First");
    model.addElement("Last");
    int size = model.getSize();
    model.insertElementAt("Middle", size / 2);
    size = model.getSize();
    if (size != 0)
      model.set(0, "New First");
    size = model.getSize();
    if (size != 0)
      model.setElementAt("New Last", size - 1);
    for (int i = 0, n = labels.length; i < n; i++) {
      model.addElement(labels[i]);
    }
    model.clear();
    size = model.getSize();
    if (size != 0)
      model.remove(0);

    model.removeAllElements();
    model.removeElement("Last");
    size = model.getSize();
    if (size != 0)
      model.removeElementAt(size / 2);
    size = model.getSize();
    if (size != 0)
      model.removeRange(0, size / 2);
    frame.setSize(640, 300);
    frame.setVisible(true);
  }
}