![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Editable Combo BoxA combo box can be configured so the user can type an entry into the top portion of the control. This allows the user to enter his choice without selecting from the list. By default, a combo box is not editable. It can be made so by passing true to the setEditable method. The isEditable method returns the current state of the editable property. Similarly to how the painting of items in the combo box is delegated to a renderer, editing is delegated to an editor. The editors responsibilities are defined in the ComboBoxEditor interface, shown in Listing 10.13. Listing 10.13 The ComboBoxEditor Interfacepublic interface ComboBoxEditor { /** * Return the component that should be added to the tree hierarchy for * this editor **/ public Component getEditorComponent(); /** * Set the item that should be edited. Cancel any editing if necessary **/ public void setItem(Object anObject); /** * Return the edited item **/ public Object getItem(); /** * Ask the editor to start editing and to select everything **/ public void selectAll(); /** * Add an ActionListener. An action event is generated when * the edited item changes **/ public void addActionListener(ActionListener l); /** * Remove an ActionListener **/ public void removeActionListener(ActionListener l); } Querying the selected item for an editable combo box is slightly more complex than it is for a non-editable combo box. The methods typically used, getSelectedItem and getSelectedIndex, are the same. However, the user may type any value into the combo box editor, including elements that are not in the list of choices in the ComboBoxModel. In this case, the getSelectedIndex method returns 1. The getSelectedItem method returns exactly what the user entered into the editor, whether this string is in the model or not. This behavior is demonstrated in the EditableComboBox application, shown in Listing 10.14. Listing 10.14 The EditableComboBoxTest Application package com.foley.test; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import com.foley.utility.ApplicationFrame; /** * An application that displays an editable JComboBox instance * in its frame. * * @author Mike Foley **/ public class EditableComboBoxTest extends Object { /** * Application entry point. * Create a frame, the combo box and display it. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { String[] data = {Apple, Orange, Grape, Banana, Mango, Pineapple, Peach, Pear }; JFrame frame = new ApplicationFrame( Editable Combobox Test ); final JComboBox combobox = new JComboBox( data ); combobox.setEditable( true ); combobox.setSelectedIndex( -1 ); combobox.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.out.println( combobox.getSelectedIndex() + + combobox.getSelectedItem() ); } } ); combobox.setBorder( BorderFactory.createLoweredBevelBorder() ); frame.getContentPane().add( combobox, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } // main } // EditableComboBoxTest In this example, a JComboBox instance is created. The setEditable method is called to enable editing. The setSelectedIndex method is passed the value of 1 to clear the selection in the combo box. This way, there is no value in the combo box when it is first displayed. An ActionListener is added to the combo box to receive notification when the selected item in the combo box changes. When a notification is received, the selected item and its index are printed to the console. The application is shown in Figure 10.9.
The first panel of Figure 10.9 shows the combo box from the EditableComboBoxTest application before a selection has been made. The editor paints differently than the non-editable combo box shown in Figure 10.7. This gives the user visual feedback that the combo box is in fact editable. The second panel shows the combo box after the item labeled Grape has been selected. The output from the application is shown in Figure 10.10. The first line is the result from ActionListener added to the combo box. The item is Grape and its index is 2. The final slide shows the combo box after editing to contain the string Bean. Figure 10.10 shows the result from ActionListener. This time, the item is not contained in the ComboBoxModel, so its index returned from the getSelectedIndex method is 1. The ActionEvent is fired when the user presses the Enter key after changing the text, or when the editor loses the input focus.
|
![]() |
|