| |
14. 43. 3. 监听改变项目, JList组件 |
|
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
public class Main {
public static void main(String[] argv) throws Exception {
JList list = new JList();
// Register a list data listener
DefaultListModel model = (DefaultListModel) list.getModel();
model.addListDataListener(new MyListDataListener());
}
}
class MyListDataListener implements ListDataListener {
public void intervalAdded(ListDataEvent evt) {
DefaultListModel model = (DefaultListModel) evt.getSource();
int start = evt.getIndex0();
int end = evt.getIndex1();
int count = end - start + 1;
for (int i = start; i <= end; i++) {
Object item = model.getElementAt(i);
}
}
public void intervalRemoved(ListDataEvent evt) {
int start = evt.getIndex0();
int end = evt.getIndex1();
int count = end - start + 1;
}
public void contentsChanged(ListDataEvent evt) {
DefaultListModel model = (DefaultListModel) evt.getSource();
int start = evt.getIndex0();
int end = evt.getIndex1();
int count = end - start + 1;
for (int i = start; i <= end; i++) {
Object item = model.getElementAt(i);
}
}
}
|
|
|