JList: ListModel and ListSelectionModel. The ListModel handles data. ListSelectionModel works with the GUI. : JList Model « Swing « Java Tutorial






import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class List {
  public static void main(String[] args) {
    final DefaultListModel model = new DefaultListModel();
    final JList list = new JList(model);
    JFrame f = new JFrame();
    f.setTitle("JList models");
    
    model.addElement("A");
    model.addElement("B");
    model.addElement("C");
    model.addElement("D");
    model.addElement("E");

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();

    leftPanel.setLayout(new BorderLayout());
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    list.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
          int index = list.locationToIndex(e.getPoint());
          Object item = model.getElementAt(index);
          String text = JOptionPane.showInputDialog("Rename item", item);
          String newitem = "";
          if (text != null)
            newitem = text.trim();
          else
            return;

          if (!newitem.isEmpty()) {
            model.remove(index);
            model.add(index, newitem);
            ListSelectionModel selmodel = list.getSelectionModel();
            selmodel.setLeadSelectionIndex(index);
          }
        }
      }
    });
    leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    leftPanel.add(new JScrollPane(list));

    JButton removeall = new JButton("Remove All");
    JButton add = new JButton("Add");
    JButton rename = new JButton("Rename");
    JButton delete = new JButton("Delete");

    add.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String text = JOptionPane.showInputDialog("Add a new item");
        String item = null;

        if (text != null)
          item = text.trim();
        else
          return;

        if (!item.isEmpty())
          model.addElement(item);
      }
    });

    delete.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        ListSelectionModel selmodel = list.getSelectionModel();
        int index = selmodel.getMinSelectionIndex();
        if (index >= 0)
          model.remove(index);
      }

    });

    rename.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        ListSelectionModel selmodel = list.getSelectionModel();
        int index = selmodel.getMinSelectionIndex();
        if (index == -1)
          return;
        Object item = model.getElementAt(index);
        String text = JOptionPane.showInputDialog("Rename item", item);
        String newitem = null;

        if (text != null) {
          newitem = text.trim();
        } else
          return;

        if (!newitem.isEmpty()) {
          model.remove(index);
          model.add(index, newitem);
        }
      }
    });

    removeall.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        model.clear();
      }
    });

    rightPanel.add(add);
    rightPanel.add(rename);
    rightPanel.add(delete);
    rightPanel.add(removeall);

    rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

    panel.add(leftPanel);
    panel.add(rightPanel);

    f.add(panel);

    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

  }
}








14.42.JList Model
14.42.1.SortedListModel: sortable JListSortedListModel: sortable JList
14.42.2.extends DefaultListModel
14.42.3.extends AbstractListModelextends AbstractListModel
14.42.4.JList: ListModel and ListSelectionModel. The ListModel handles data. ListSelectionModel works with the GUI.
14.42.5.Create a list that allows adds and removes
14.42.6.Append an item
14.42.7.Insert an item at the beginning
14.42.8.Adding and Removing an Item in a JList Component
14.42.9.Set method replaces an item
14.42.10.Methods are used to remove items
14.42.11.DefaultListModel and JList
14.42.12.Getting the Items in a JList Component