FileDocCategorySizeDatePackage
ItemChooser.javaAPI DocExample11208Sat Jan 24 10:44:32 GMT 2004je3.gui

ItemChooser

public class ItemChooser extends JPanel
This class is a Swing component that presents a choice to the user. It allows the choice to be presented in a JList, in a JComboBox, or with a bordered group of JRadioButton components. Additionally, it displays the name of the choice with a JLabel. It allows an arbitrary value to be associated with each possible choice. Note that this component only allows one item to be selected at a time. Multiple selections are not supported.

Fields Summary
String
name
String[]
labels
Object[]
values
int
selection
int
presentation
public static final int
LIST
public static final int
COMBOBOX
public static final int
RADIOBUTTONS
JList
list
JComboBox
combobox
JRadioButton[]
radiobuttons
ArrayList
listeners
Constructors Summary
public ItemChooser(String name, String[] labels, Object[] values, int defaultSelection, int presentation)


    // The constructor method sets everything up
          
		          
    
	// Copy the constructor arguments to instance fields
	this.name = name;
	this.labels = labels;
	this.values = values;
	this.selection = defaultSelection;
	this.presentation = presentation;

	// If no values were supplied, use the labels
	if (values == null) this.values = labels;

	// Now create content and event handlers based on presentation type
	switch(presentation) {
	case LIST: initList(); break;
	case COMBOBOX: initComboBox(); break;
	case RADIOBUTTONS: initRadioButtons(); break;
	}
    
Methods Summary
public voidaddItemChooserListener(je3.gui.ItemChooser$Listener l)

	listeners.add(l);
    
public java.lang.String[]getLabels()

 return labels; 
public java.lang.StringgetName()

 return name; 
public intgetPresentation()

 return presentation; 
public intgetSelectedIndex()
Return the index of the selected item

 return selection; 
public java.lang.ObjectgetSelectedValue()
Return the object associated with the selected item

 return values[selection]; 
public java.lang.Object[]getValues()

 return values; 
voidinitComboBox()

	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);
    
voidinitList()

	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
    
voidinitRadioButtons()

	// 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 voidremoveItemChooserListener(je3.gui.ItemChooser$Listener l)

	listeners.remove(l);
    
protected voidselect(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 voidsetSelectedIndex(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;