import java.awt.BorderLayout; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class ListenElementErmitteln implements ListSelectionListener { JList liste; public ListenElementErmitteln() { DefaultListModel model = new DefaultListModel(); model.addElement("eins"); model.addElement("zwei"); model.addElement("drei"); liste = new JList(model); liste.addListSelectionListener(this); JScrollPane sp = new JScrollPane(liste); JFrame frame = new JFrame("Listenelement ermitteln"); frame.getContentPane().add(sp, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocation(200, 200); frame.setVisible(true); } public void valueChanged(ListSelectionEvent e) { if (liste.isSelectionEmpty()) { return; } int minIndex = liste.getMinSelectionIndex(); int maxIndex = liste.getMaxSelectionIndex(); for (int i = minIndex; i <= maxIndex; i++) { if (liste.isSelectedIndex(i) && e.getValueIsAdjusting()) { System.out.println(liste.getModel().getElementAt(i)); } } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new ListenElementErmitteln()); } }