ListSelectionEvent : ListSelectionListener « Swing Event « Java Tutorial






import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Main extends JFrame {

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String items[] = { "A", "B", "C", "D" };
    JList list = new JList(items);
    ListSelectionModel selModel = list.getSelectionModel();

    selModel.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
          System.out.println("selection changed: " + e.getFirstIndex());
        }
      }
    });
    getContentPane().add(list);
    pack();
    setVisible(true);
  }

  public static void main(String[] args) {
    Main m = new Main();
  }
}








15.24.ListSelectionListener
15.24.1.Listening to JList Events with a ListSelectionListenerListening to JList Events with a ListSelectionListener
15.24.2.Shared ListSelectionHandlerShared ListSelectionHandler
15.24.3.ListSelectionEvent
15.24.4.ListSelectionListener and ListSelectionEvent
15.24.5.ListSelectionModel, JTable and ListSelectionListener
15.24.6.ListSelectionModel and ListSelectionListener
15.24.7.Implement a graphical list selection monitor