Java Reference
In-Depth Information
The indices aren't necessarily ordered, and neither are the bounds of the altered region. In
the case of the list model contents changing, not everything within the region may have been
altered. The area whose contents did change is the bounded region specified by the indices.
The type property setting is one of three constants, as shown in Table 13-3, that map directly to
the specific interface method called.
Table 13-3. ListDataEvent Type Constants
Type Constant
Method
CONTENTS_CHANGED
contentsChanged()
INTERVAL_ADDED
intervalAdded()
INTERVAL_REMOVED
intervalRemoved()
If any ListDataListener objects are attached to the data model when any one of the oper-
ational methods of the DefaultListModel class are called, each of the listeners will be notified
of the data model change. To demonstrate the use of ListDataListener and the dynamic
updating of the data model, the ModifyModelSample program shown in Listing 13-1 uses all the
DefaultListModel class modifying methods, sending the output in the form of the event and
list contents to a JTextArea .
Listing 13-1. Modifying the Data Model
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Enumeration;
public class ModifyModelSample {
static String labels[] = {"Chardonnay", "Sauvignon", "Riesling", "Cabernet",
"Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc", "Syrah",
"Gewürztraminer"};
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
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]);
}
Search WWH ::




Custom Search