Methods Summary |
---|
public void | addItemChooserListener(je3.gui.ItemChooser$Listener l)
listeners.add(l);
|
public java.lang.String[] | getLabels() return labels;
|
public java.lang.String | getName() return name;
|
public int | getPresentation() return presentation;
|
public int | getSelectedIndex()Return the index of the selected item return selection;
|
public java.lang.Object | getSelectedValue()Return the object associated with the selected item return values[selection];
|
public java.lang.Object[] | getValues() return values;
|
void | initComboBox()
combobox = new JComboBox(labels); // Create the combo box
combobox.setSelectedIndex(selection); // Set initial state
// Handle changes to the state
combobox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
ItemChooser.this.select(combobox.getSelectedIndex());
}
});
// Lay out combo box and name label horizontally
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.add(new JLabel(name));
this.add(combobox);
|
void | initList()
list = new JList(labels); // Create the list
list.setSelectedIndex(selection); // Set initial state
// Handle state changes
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
ItemChooser.this.select(list.getSelectedIndex());
}
});
// Lay out list and name label vertically
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // vertical
this.add(new JLabel(name)); // Display choice name
this.add(new JScrollPane(list)); // Add the JList
|
void | initRadioButtons()
// Create an array of mutually exclusive radio buttons
radiobuttons = new JRadioButton[labels.length]; // the array
ButtonGroup radioButtonGroup = new ButtonGroup(); // used for exclusion
ChangeListener listener = new ChangeListener() { // A shared listener
public void stateChanged(ChangeEvent e) {
JRadioButton b = (JRadioButton)e.getSource();
if (b.isSelected()) {
// If we received this event because a button was
// selected, then loop through the list of buttons to
// figure out the index of the selected one.
for(int i = 0; i < radiobuttons.length; i++) {
if (radiobuttons[i] == b) {
ItemChooser.this.select(i);
return;
}
}
}
}
};
// Display the choice name in a border around the buttons
this.setBorder(new TitledBorder(new EtchedBorder(), name));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// Create the buttons, add them to the button group, and specify
// the event listener for each one.
for(int i = 0; i < labels.length; i++) {
radiobuttons[i] = new JRadioButton(labels[i]);
if (i == selection) radiobuttons[i].setSelected(true);
radiobuttons[i].addChangeListener(listener);
radioButtonGroup.add(radiobuttons[i]);
this.add(radiobuttons[i]);
}
|
public void | removeItemChooserListener(je3.gui.ItemChooser$Listener l)
listeners.remove(l);
|
protected void | select(int selection)This internal method is called when the selection changes. It stores
the new selected index, and fires events to any registered listeners.
The event listeners registered on the JList, JComboBox, or JRadioButtons
all call this method.
this.selection = selection; // Store the new selected index
if (!listeners.isEmpty()) { // If there are any listeners registered
// Create an event object to describe the selection
ItemChooser.Event e =
new ItemChooser.Event(this, selection, values[selection]);
// Loop through the listeners using an Iterator
for(Iterator i = listeners.iterator(); i.hasNext();) {
ItemChooser.Listener l = (ItemChooser.Listener)i.next();
l.itemChosen(e); // Notify each listener of the selection
}
}
|
public void | setSelectedIndex(int selection)Set the selected item by specifying its index. Calling this
method changes the on-screen display but does not generate events.
switch(presentation) {
case LIST: list.setSelectedIndex(selection); break;
case COMBOBOX: combobox.setSelectedIndex(selection); break;
case RADIOBUTTONS: radiobuttons[selection].setSelected(true); break;
}
this.selection = selection;
|