Java Tutorial/Swing Event/ListDataListener

Материал из Java эксперт
Версия от 15:18, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

implements ListDataListener

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()); 
  }
  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 UsingListDataListener {
  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);
    // Fill model
    final DefaultListModel model = new DefaultListModel();
    
    model.addListDataListener(new MyListDataListener());
    for (int i = 0, n = labels.length; i < n; i++) {
      model.addElement(labels[i]);
    }
    JList jlist = new JList(model);
    
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);
    frame.setSize(640, 300);
    frame.setVisible(true);
  }
}





Using ListDataListener to listen

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
public class ModifyModelSampleListDataListener {
  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);
    // Fill model
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
      model.addElement(labels[i]);
    }
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);
    
    ListDataListener listDataListener = new ListDataListener() {
      public void contentsChanged(ListDataEvent listDataEvent) {
        appendEvent(listDataEvent);
      }
      public void intervalAdded(ListDataEvent listDataEvent) {
        appendEvent(listDataEvent);
      }
      public void intervalRemoved(ListDataEvent listDataEvent) {
        appendEvent(listDataEvent);
      }
      private void appendEvent(ListDataEvent listDataEvent) {
        switch (listDataEvent.getType()) {
        case ListDataEvent.CONTENTS_CHANGED:
          System.out.println("Type: Contents Changed");
          break;
        case ListDataEvent.INTERVAL_ADDED:
          System.out.println("Type: Interval Added");
          break;
        case ListDataEvent.INTERVAL_REMOVED:
          System.out.println("Type: Interval Removed");
          break;
        }
        System.out.println(", Index0: " + listDataEvent.getIndex0());
        System.out.println(", Index1: " + listDataEvent.getIndex1());
        DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource();
        System.out.println(theModel);
      }
    };
    model.addListDataListener(listDataListener);
    
    JButton jb = new JButton("add F");
    jb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        model.add(0, "First");
      }
    });
    
    frame.add(jb,BorderLayout.SOUTH);    
    
    frame.setSize(640, 300);
    frame.setVisible(true);
  }
}