Java Reference
In-Depth Information
JList jlist = new JList(model);
JScrollPane scrollPane1 = new JScrollPane(jlist);
frame.add(scrollPane1, BorderLayout.WEST);
final JTextArea textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane2 = new JScrollPane(textArea);
frame.add(scrollPane2, 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) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
switch (listDataEvent.getType()) {
case ListDataEvent.CONTENTS_CHANGED:
pw.print("Type: Contents Changed");
break;
case ListDataEvent.INTERVAL_ADDED:
pw.print("Type: Interval Added");
break;
case ListDataEvent.INTERVAL_REMOVED:
pw.print("Type: Interval Removed");
break;
}
pw.print(", Index0: " + listDataEvent.getIndex0());
pw.print(", Index1: " + listDataEvent.getIndex1());
DefaultListModel theModel =
(DefaultListModel)listDataEvent.getSource();
pw.println(theModel);
textArea.append(sw.toString());
}
};
model.addListDataListener(listDataListener);
Search WWH ::




Custom Search