FileDocCategorySizeDatePackage
BasicComboBoxEditor.javaAPI DocJava SE 5 API4211Fri Aug 26 14:58:02 BST 2005javax.swing.plaf.basic

BasicComboBoxEditor

public class BasicComboBoxEditor extends Object implements ComboBoxEditor, FocusListener
The default editor for editable combo boxes. The editor is implemented as a JTextField.
version
1.26 12/19/03
author
Arnaud Weber
author
Mark Davidson

Fields Summary
protected JTextField
editor
private Object
oldValue
Constructors Summary
public BasicComboBoxEditor()

        editor = new BorderlessTextField("",9);
        editor.setBorder(null);
    
Methods Summary
public voidaddActionListener(java.awt.event.ActionListener l)

        editor.addActionListener(l);
    
public voidfocusGained(java.awt.event.FocusEvent e)

public voidfocusLost(java.awt.event.FocusEvent e)

public java.awt.ComponentgetEditorComponent()

        return editor;
    
public java.lang.ObjectgetItem()

        Object newValue = editor.getText();
        
        if (oldValue != null && !(oldValue instanceof String))  {
            // The original value is not a string. Should return the value in it's
            // original type.
            if (newValue.equals(oldValue.toString()))  {
                return oldValue;
            } else {
                // Must take the value from the editor and get the value and cast it to the new type.
                Class cls = oldValue.getClass();
                try {
                    Method method = cls.getMethod("valueOf", new Class[]{String.class});
                    newValue = method.invoke(oldValue, new Object[] { editor.getText()});
                } catch (Exception ex) {
                    // Fail silently and return the newValue (a String object)
                }
            }
        }
        return newValue;
    
public voidremoveActionListener(java.awt.event.ActionListener l)

        editor.removeActionListener(l);
    
public voidselectAll()

        editor.selectAll();
        editor.requestFocus();
    
public voidsetItem(java.lang.Object anObject)
Sets the item that should be edited.

param
anObject the displayed value of the editor

        if ( anObject != null )  {
            editor.setText(anObject.toString());
            
            oldValue = anObject;
        } else {
            editor.setText("");
        }