Removing all the elements of a JList

The quickest way to find an answer to these kind of questions is to look at the API. You can call either removeAllElements or clear on the list model. clear is preferred.

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
      final DefaultListModel listModel = new DefaultListModel();   
      // populate listmodel
      for (int i=0; i<5; i++) {
         listModel.addElement("list item #" + i);
      final JList list = new JList(listModel); 
      getContentPane().add(new JScrollPane(list));    
      JButton removeAllButton = new JButton("Remove all elements");
      removeAllButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
   public static void main(String[] args) {
      (new Main()).show();