Listening for Changes to the Selection in a JList Component
import javax.swing.JList; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class Main { public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); list.addListSelectionListener(new MyListSelectionListener()); } } class MyListSelectionListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent evt) { if (!evt.getValueIsAdjusting()) { JList list = (JList) evt.getSource(); Object[] selected = list.getSelectedValues(); for (int i = 0; i < selected.length; i++) { Object sel = selected[i]; } } } }