ModifyModelSample: ListModel Demo
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
public class ModifyModelSample {
static String labels[] = { "Chardonnay", "Sauvignon", "Riesling",
"Cabernet", "Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc",
"Syrah", "Gewurztraminer" };
public static void main(String args[]) {
JFrame frame = new JFrame("Modifying Model");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
// 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);
contentPane.add(scrollPane1, BorderLayout.WEST);
final JTextArea textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane2 = new JScrollPane(textArea);
contentPane.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();
Enumeration elements = theModel.elements();
pw.print(", Elements: ");
while (elements.hasMoreElements()) {
pw.print(elements.nextElement());
pw.print(",");
}
pw.println();
textArea.append(sw.toString());
}
};
model.addListDataListener(listDataListener);
// Setup buttons
JPanel jp = new JPanel(new GridLayout(2, 1));
JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
jp.add(jp1);
jp.add(jp2);
JButton jb = new JButton("add F");
jp1.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
model.add(0, "First");
}
});
jb = new JButton("addElement L");
jp1.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
model.addElement("Last");
}
});
jb = new JButton("insertElementAt M");
jp1.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int size = model.getSize();
model.insertElementAt("Middle", size / 2);
}
});
jb = new JButton("set F");
jp1.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int size = model.getSize();
if (size != 0)
model.set(0, "New First");
}
});
jb = new JButton("setElementAt L");
jp1.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int size = model.getSize();
if (size != 0)
model.setElementAt("New Last", size - 1);
}
});
jb = new JButton("load 10");
jp1.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
for (int i = 0, n = labels.length; i < n; i++) {
model.addElement(labels[i]);
}
}
});
jb = new JButton("clear");
jp2.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
model.clear();
}
});
jb = new JButton("remove F");
jp2.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int size = model.getSize();
if (size != 0)
model.remove(0);
}
});
jb = new JButton("removeAllElements");
jp2.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
model.removeAllElements();
}
});
jb = new JButton("removeElement 'Last'");
jp2.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
model.removeElement("Last");
}
});
jb = new JButton("removeElementAt M");
jp2.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int size = model.getSize();
if (size != 0)
model.removeElementAt(size / 2);
}
});
jb = new JButton("removeRange FM");
jp2.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int size = model.getSize();
if (size != 0)
model.removeRange(0, size / 2);
}
});
contentPane.add(jp, BorderLayout.SOUTH);
frame.setSize(640, 300);
frame.setVisible(true);
}
}
Related examples in the same category