Java Swing Tutorial - Java DefaultListModel.clear()








Syntax

DefaultListModel.clear() has the following syntax.

public void clear()

Example

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

import javax.swing.DefaultListModel;
import javax.swing.JList;
//  www  .  ja v  a 2s .c  o  m
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();
  }
}