Java Swing ListDataListener handle list data event

Description

Java Swing ListDataListener handle list data event

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Main extends JPanel implements ListSelectionListener {
  private JList list;
  private DefaultListModel listModel;

  private static final String addString = "Add";
  private static final String deleteString = "Delete";
  private static final String upString = "Move up";
  private static final String downString = "Move down";

  private JButton addButton;
  private JButton deleteButton;
  private JButton upButton;
  private JButton downButton;

  private JTextField nameField;
  public Main() {
    super(new BorderLayout());

    // Create and populate the list model.
    listModel = new DefaultListModel();
    listModel.addElement("A");
    listModel.addElement("B");
    listModel.addElement("C");
    listModel.addElement("D");
    listModel.addElement("E");
    listModel.addElement("F");
    listModel.addElement("G");
    listModel.addElement("H");
    listModel.addElement("I");
    listModel.addElement("J");
    listModel.addElement("K");
    listModel.addListDataListener(new MyListDataListener());

    // Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    list.setSelectedIndex(0);// ww  w.j  a v  a2 s . co  m
    list.addListSelectionListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    // Create the list-modifying buttons.
    addButton = new JButton(addString);
    addButton.setActionCommand(addString);
    addButton.addActionListener(new AddButtonListener());

    deleteButton = new JButton(deleteString);
    deleteButton.setActionCommand(deleteString);
    deleteButton.addActionListener(new DeleteButtonListener());

    upButton = new JButton("Move up");

    upButton.setActionCommand(upString);
    upButton.addActionListener(new UpDownListener());

    downButton = new JButton("Move down");

    downButton.setActionCommand(downString);
    downButton.addActionListener(new UpDownListener());

    JPanel upDownPanel = new JPanel(new GridLayout(2, 1));
    upDownPanel.add(upButton);
    upDownPanel.add(downButton);

    nameField = new JTextField(15);
    nameField.addActionListener(new AddButtonListener());
    String name = listModel.getElementAt(list.getSelectedIndex()).toString();
    nameField.setText(name);

    JPanel buttonPane = new JPanel();
    buttonPane.add(nameField);
    buttonPane.add(addButton);
    buttonPane.add(deleteButton);
    buttonPane.add(upDownPanel);


    // Put everything together.
    add(buttonPane, BorderLayout.PAGE_START);
    add(listScrollPane, BorderLayout.CENTER);
  }

  class MyListDataListener implements ListDataListener {
    public void contentsChanged(ListDataEvent e) {
      System.out.println("contentsChanged: " + e.getIndex0() + ", " + e.getIndex1());
    }

    public void intervalAdded(ListDataEvent e) {
       System.out.println("intervalAdded: " + e.getIndex0() + ", " + e.getIndex1());
    }

    public void intervalRemoved(ListDataEvent e) {
       System.out.println("intervalRemoved: " + e.getIndex0() + ", " + e.getIndex1());
    }
  }

  class DeleteButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      ListSelectionModel lsm = list.getSelectionModel();
      int firstSelected = lsm.getMinSelectionIndex();
      int lastSelected = lsm.getMaxSelectionIndex();
      listModel.removeRange(firstSelected, lastSelected);

      int size = listModel.size();
      if (size == 0) {
        deleteButton.setEnabled(false);
        upButton.setEnabled(false);
        downButton.setEnabled(false);

      } else {
        if (firstSelected == listModel.getSize()) {
          firstSelected--;
        }
        list.setSelectedIndex(firstSelected);
      }
    }
  }

  class AddButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (nameField.getText().equals("")) {
        return;
      }

      int index = list.getSelectedIndex();
      int size = listModel.getSize();

      if (index == -1 || (index + 1 == size)) {
        listModel.addElement(nameField.getText());
        list.setSelectedIndex(size);
      } else {
        listModel.insertElementAt(nameField.getText(), index + 1);
        list.setSelectedIndex(index + 1);
      }
    }
  }
  class UpDownListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      int moveMe = list.getSelectedIndex();
      if (e.getActionCommand().equals(upString)) {
        if (moveMe != 0) {
          swap(moveMe, moveMe - 1);
          list.setSelectedIndex(moveMe - 1);
          list.ensureIndexIsVisible(moveMe - 1);
        }
      } else {
        if (moveMe != listModel.getSize() - 1) {
          swap(moveMe, moveMe + 1);
          list.setSelectedIndex(moveMe + 1);
          list.ensureIndexIsVisible(moveMe + 1);
        }
      }
    }
  }
  private void swap(int a, int b) {
    Object aObject = listModel.getElementAt(a);
    Object bObject = listModel.getElementAt(b);
    listModel.set(a, bObject);
    listModel.set(b, aObject);
  }

  public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting() == false) {
      if (list.getSelectedIndex() == -1) {
        deleteButton.setEnabled(false);
        upButton.setEnabled(false);
        downButton.setEnabled(false);
        nameField.setText("");
      } else if (list.getSelectedIndices().length > 1) {
        deleteButton.setEnabled(true);
        upButton.setEnabled(false);
        downButton.setEnabled(false);
      } else {
        deleteButton.setEnabled(true);
        upButton.setEnabled(true);
        downButton.setEnabled(true);
        nameField.setText(list.getSelectedValue().toString());
      }
    }
  }

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

     JComponent newContentPane = new Main();
     frame.setContentPane(newContentPane);

     newContentPane.setMinimumSize(new Dimension(newContentPane
         .getPreferredSize().width, 100));

     frame.pack();
     frame.setVisible(true);
  }
}



PreviousNext

Related