/*
Code revised from Desktop Java Live:
http://www.sourcebeat.com/downloads/
*/
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import com.jgoodies.binding.adapter.Bindings;
import com.jgoodies.binding.list.ArrayListModel;
import com.jgoodies.binding.list.SelectionInList;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
public class SelectionInListModelExample extends JPanel {
private ArrayListModel arrayListModel;
private int counter = 1;
public SelectionInListModelExample() {
DefaultFormBuilder defaultFormBuilder = new DefaultFormBuilder(new FormLayout("p, 2dlu, p:g"));
defaultFormBuilder.setDefaultDialogBorder();
this.arrayListModel = new ArrayListModel();
this.arrayListModel.add("Swing");
this.arrayListModel.add("SWT");
this.arrayListModel.add("HTML");
this.arrayListModel.add("Flash");
this.arrayListModel.add("QT");
SelectionInList selectionInList = new SelectionInList((ListModel) this.arrayListModel);
ExamplePropertyChangeListener propertyChangeListener = new ExamplePropertyChangeListener();
selectionInList.addPropertyChangeListener(propertyChangeListener);
ExampleListDataChangeListener listDataChangeListener = new ExampleListDataChangeListener();
selectionInList.addListDataListener(listDataChangeListener);
JList jlist = new JList();
Bindings.bind(jlist, selectionInList);
defaultFormBuilder.append("List Model: ", new JScrollPane(jlist));
defaultFormBuilder.append("", new JButton(new AddElementAction(this.arrayListModel)));
defaultFormBuilder.append("", new JButton(new RemoveFirstElementAction(this.arrayListModel)));
add(defaultFormBuilder.getPanel());
}
private class RemoveFirstElementAction extends AbstractAction {
private List targetList;
public RemoveFirstElementAction(List targetList) {
super("Remove First Element");
this.targetList = targetList;
}
public void actionPerformed(ActionEvent e) {
if (this.targetList.size() > 0) {
this.targetList.remove(0);
}
}
}
private class AddElementAction extends AbstractAction {
private List targetList;
public AddElementAction(List targetList) {
super("Add Element");
this.targetList = targetList;
}
public void actionPerformed(ActionEvent e) {
this.targetList.add("New Element " + counter);
counter++;
}
}
private class ExamplePropertyChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
JOptionPane.showMessageDialog(null, "<html>Property <b>" + evt.getPropertyName() + "</b> changed Old Value = " + evt.getOldValue() + ", New Value = " + evt.getNewValue() + "</html>");
}
}
private class ExampleListDataChangeListener implements ListDataListener {
public void intervalAdded(ListDataEvent e) {
JOptionPane.showMessageDialog(null, "<html>Interval " + e.getIndex0() + " to " + e.getIndex1() + " added.</html>");
}
public void intervalRemoved(ListDataEvent e) {
JOptionPane.showMessageDialog(null, "<html>Interval " + e.getIndex0() + " to " + e.getIndex1() + " removed.</html>");
}
public void contentsChanged(ListDataEvent e) {
JOptionPane.showMessageDialog(null, "<html>Contents changed.</html>");
}
}
public static void main(String[] a){
JFrame f = new JFrame("List / ListModel Example (Selection List Model)");
f.setDefaultCloseOperation(2);
f.add(new SelectionInListModelExample());
f.pack();
f.setVisible(true);
}
}
|