Java Reference
In-Depth Information
Example 10•15: ItemChooser.java (continued)
public Event(ItemChooser source,
int selectedIndex, Object selectedValue) {
super(source);
this.selectedIndex = selectedIndex;
this.selectedValue = selectedValue;
}
public ItemChooser getItemChooser() { return (ItemChooser)getSource();}
public int getSelectedIndex() { return selectedIndex; }
public Object getSelectedValue() { return selectedValue; }
}
/**
* This inner interface must be implemented by any object that wants to be
* notified when the current selection in a ItemChooser component changes.
**/
public interface Listener extends java.util.EventListener {
public void itemChosen(ItemChooser.Event e);
}
/**
* This inner class is a simple demonstration of the ItemChooser component
* It uses command-line arguments as ItemChooser labels and values.
**/
public static class Demo {
public static void main(String[] args) {
// Create a window, arrange to handle close requests
final JFrame frame = new JFrame("ItemChooser Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
// A "message line" to display results in
final JLabel msgline = new JLabel(" ");
// Create a panel holding three ItemChooser components
JPanel chooserPanel = new JPanel();
final ItemChooser c1 = new ItemChooser("Choice #1", args, null, 0,
ItemChooser.LIST);
final ItemChooser c2 = new ItemChooser("Choice #2", args, null, 0,
ItemChooser.COMBOBOX);
final ItemChooser c3 = new ItemChooser("Choice #3", args, null, 0,
ItemChooser.RADIOBUTTONS);
// An event listener that displays changes on the message line
ItemChooser.Listener l = new ItemChooser.Listener() {
public void itemChosen(ItemChooser.Event e) {
msgline.setText(e.getItemChooser().getName() + ": " +
e.getSelectedIndex() + ": " +
e.getSelectedValue());
}
};
c1.addItemChooserListener(l);
c2.addItemChooserListener(l);
c3.addItemChooserListener(l);
// Instead of tracking every change with a ItemChooser.Listener,
// applications can also just query the current state when
Search WWH ::




Custom Search