Java Swing ListDataListener implement
import java.awt.BorderLayout; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; class MyListDataListener implements ListDataListener { public void contentsChanged(ListDataEvent e) { System.out.println("contentsChanged: " + e.getIndex0() + ", " + e.getIndex1()); }// w ww .j a v a 2s. c o m 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()); } } public class Main { static String labels[] = { "A", "B", "C", "D", "E", "F", "G" }; public static void main(String args[]) { JFrame frame = new JFrame("Modifying Model"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultListModel<String> model = new DefaultListModel<>(); model.addElement("CSS"); model.addElement("HTML"); model.addElement("Java"); model.addListDataListener(new MyListDataListener()); JList<String> jlist = new JList<>(model); JScrollPane scrollPane1 = new JScrollPane(jlist); frame.add(scrollPane1, BorderLayout.CENTER); frame.setSize(640, 300); frame.setVisible(true); } }