Java by API/javax.swing/DefaultListModel

Материал из Java эксперт
Перейти к: навигация, поиск

DefaultListModel: addElement(Object obj)

 
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class MainClass {
  public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(1000);
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 5; j++) {
        model.addElement(labels[j]);
      }
    }
    JList jlist2 = new JList(model);
    jlist2.setVisibleRowCount(4);
    jlist2.setFixedCellHeight(12);
    jlist2.setFixedCellWidth(200);
    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);
    frame.setSize(300, 350);
    frame.setVisible(true);
  }
}





DefaultListModel: add(int index, Object element)

 
/*Type: Interval Added
, Index0: 0
, Index1: 0
[First, a, b, c, d]
Type: Interval Added
, Index0: 5
, Index1: 5
[First, a, b, c, d, Last]
Type: Interval Added
, Index0: 3
, Index1: 3
[First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 0
, Index1: 0
[New First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 6
, Index1: 6
[New First, a, b, Middle, c, d, New Last]
Type: Interval Added
, Index0: 7
, Index1: 7
[New First, a, b, Middle, c, d, New Last, a]
Type: Interval Added
, Index0: 8
, Index1: 8
[New First, a, b, Middle, c, d, New Last, a, b]
Type: Interval Added
, Index0: 9
, Index1: 9
[New First, a, b, Middle, c, d, New Last, a, b, c]
Type: Interval Added
, Index0: 10
, Index1: 10
[New First, a, b, Middle, c, d, New Last, a, b, c, d]
Type: Interval Removed
, Index0: 0
, Index1: 10
[]
 * */
import java.awt.BorderLayout;
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 MainClass {
  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);
  }
}





DefaultListModel: clear()

  

import javax.swing.DefaultListModel;
import javax.swing.JList;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);
    // Remove the first item
    int pos = 0;
    model.remove(pos);
    // Remove the last item
    pos = model.getSize() - 1;
    if (pos >= 0) {
      model.remove(pos);
    }
    // Remove all items
    model.clear();
  }
}





DefaultListModel: ensureCapacity(int count)

 
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class MainClass {
  public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(1000);
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 5; j++) {
        model.addElement(labels[j]);
      }
    }
    JList jlist2 = new JList(model);
    jlist2.setVisibleRowCount(4);
    jlist2.setFixedCellHeight(12);
    jlist2.setFixedCellWidth(200);
    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);
    frame.setSize(300, 350);
    frame.setVisible(true);
  }
}





DefaultListModel: getSize()

 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class MainClass extends JPanel {
  JList list;
  DefaultListModel model;
  int counter = 15;
  public MainClass() {
    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(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        model.addElement("Element " + counter);
        counter++;
      }
    });
    removeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (model.getSize() > 0)
          model.removeElementAt(0);
      }
    });
    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
  }
  public static void main(String s[]) {
    JFrame frame = new JFrame("List Model Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new MainClass());
    frame.setSize(260, 200);
    frame.setVisible(true);
  }
}





DefaultListModel: insertElementAt(Object obj, int index)

 
/*Type: Interval Added
, Index0: 0
, Index1: 0
[First, a, b, c, d]
Type: Interval Added
, Index0: 5
, Index1: 5
[First, a, b, c, d, Last]
Type: Interval Added
, Index0: 3
, Index1: 3
[First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 0
, Index1: 0
[New First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 6
, Index1: 6
[New First, a, b, Middle, c, d, New Last]
Type: Interval Added
, Index0: 7
, Index1: 7
[New First, a, b, Middle, c, d, New Last, a]
Type: Interval Added
, Index0: 8
, Index1: 8
[New First, a, b, Middle, c, d, New Last, a, b]
Type: Interval Added
, Index0: 9
, Index1: 9
[New First, a, b, Middle, c, d, New Last, a, b, c]
Type: Interval Added
, Index0: 10
, Index1: 10
[New First, a, b, Middle, c, d, New Last, a, b, c, d]
Type: Interval Removed
, Index0: 0
, Index1: 10
[]
 * */
import java.awt.BorderLayout;
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 MainClass {
  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);
  }
}





DefaultListModel: removeAllElements()

 
/*Type: Interval Added
, Index0: 0
, Index1: 0
[First, a, b, c, d]
Type: Interval Added
, Index0: 5
, Index1: 5
[First, a, b, c, d, Last]
Type: Interval Added
, Index0: 3
, Index1: 3
[First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 0
, Index1: 0
[New First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 6
, Index1: 6
[New First, a, b, Middle, c, d, New Last]
Type: Interval Added
, Index0: 7
, Index1: 7
[New First, a, b, Middle, c, d, New Last, a]
Type: Interval Added
, Index0: 8
, Index1: 8
[New First, a, b, Middle, c, d, New Last, a, b]
Type: Interval Added
, Index0: 9
, Index1: 9
[New First, a, b, Middle, c, d, New Last, a, b, c]
Type: Interval Added
, Index0: 10
, Index1: 10
[New First, a, b, Middle, c, d, New Last, a, b, c, d]
Type: Interval Removed
, Index0: 0
, Index1: 10
[]
 * */
import java.awt.BorderLayout;
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 MainClass {
  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);
  }
}





DefaultListModel: removeElementAt(int index)

 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class MainClass extends JPanel {
  JList list;
  DefaultListModel model;
  int counter = 15;
  public MainClass() {
    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(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        model.addElement("Element " + counter);
        counter++;
      }
    });
    removeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (model.getSize() > 0)
          model.removeElementAt(0);
      }
    });
    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
  }
  public static void main(String s[]) {
    JFrame frame = new JFrame("List Model Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new MainClass());
    frame.setSize(260, 200);
    frame.setVisible(true);
  }
}





DefaultListModel: remove(int index)

 
/*Type: Interval Added
, Index0: 0
, Index1: 0
[First, a, b, c, d]
Type: Interval Added
, Index0: 5
, Index1: 5
[First, a, b, c, d, Last]
Type: Interval Added
, Index0: 3
, Index1: 3
[First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 0
, Index1: 0
[New First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 6
, Index1: 6
[New First, a, b, Middle, c, d, New Last]
Type: Interval Added
, Index0: 7
, Index1: 7
[New First, a, b, Middle, c, d, New Last, a]
Type: Interval Added
, Index0: 8
, Index1: 8
[New First, a, b, Middle, c, d, New Last, a, b]
Type: Interval Added
, Index0: 9
, Index1: 9
[New First, a, b, Middle, c, d, New Last, a, b, c]
Type: Interval Added
, Index0: 10
, Index1: 10
[New First, a, b, Middle, c, d, New Last, a, b, c, d]
Type: Interval Removed
, Index0: 0
, Index1: 10
[]
 * */
import java.awt.BorderLayout;
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 MainClass {
  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);
  }
}





DefaultListModel: removeRange(int fromIndex, int toIndex)

 
/*Type: Interval Added
, Index0: 0
, Index1: 0
[First, a, b, c, d]
Type: Interval Added
, Index0: 5
, Index1: 5
[First, a, b, c, d, Last]
Type: Interval Added
, Index0: 3
, Index1: 3
[First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 0
, Index1: 0
[New First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 6
, Index1: 6
[New First, a, b, Middle, c, d, New Last]
Type: Interval Added
, Index0: 7
, Index1: 7
[New First, a, b, Middle, c, d, New Last, a]
Type: Interval Added
, Index0: 8
, Index1: 8
[New First, a, b, Middle, c, d, New Last, a, b]
Type: Interval Added
, Index0: 9
, Index1: 9
[New First, a, b, Middle, c, d, New Last, a, b, c]
Type: Interval Added
, Index0: 10
, Index1: 10
[New First, a, b, Middle, c, d, New Last, a, b, c, d]
Type: Interval Removed
, Index0: 0
, Index1: 10
[]
 * */
import java.awt.BorderLayout;
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 MainClass {
  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);
  }
}





DefaultListModel: setElementAt(Object obj, int index)

 
/*Type: Interval Added
, Index0: 0
, Index1: 0
[First, a, b, c, d]
Type: Interval Added
, Index0: 5
, Index1: 5
[First, a, b, c, d, Last]
Type: Interval Added
, Index0: 3
, Index1: 3
[First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 0
, Index1: 0
[New First, a, b, Middle, c, d, Last]
Type: Contents Changed
, Index0: 6
, Index1: 6
[New First, a, b, Middle, c, d, New Last]
Type: Interval Added
, Index0: 7
, Index1: 7
[New First, a, b, Middle, c, d, New Last, a]
Type: Interval Added
, Index0: 8
, Index1: 8
[New First, a, b, Middle, c, d, New Last, a, b]
Type: Interval Added
, Index0: 9
, Index1: 9
[New First, a, b, Middle, c, d, New Last, a, b, c]
Type: Interval Added
, Index0: 10
, Index1: 10
[New First, a, b, Middle, c, d, New Last, a, b, c, d]
Type: Interval Removed
, Index0: 0
, Index1: 10
[]
 * */
import java.awt.BorderLayout;
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 MainClass {
  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);
  }
}





DefaultListModel: set(int index, Object element)

  
import javax.swing.DefaultListModel;
import javax.swing.JList;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);
    // Replace the 2nd item
    int pos = 1;
    model.set(pos, "b");
  }
}