FileDocCategorySizeDatePackage
BasicComboBoxEditor.javaAPI DocJava SE 6 API4636Tue Jun 10 00:26:46 BST 2008javax.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.28 05/25/06
author
Arnaud Weber
author
Mark Davidson

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

        editor = createEditorComponent();
    
Methods Summary
public voidaddActionListener(java.awt.event.ActionListener l)

        editor.addActionListener(l);
    
protected javax.swing.JTextFieldcreateEditorComponent()
Creates the internal editor component. Override this to provide a custom implementation.

return
a new editor component
since
1.6

        JTextField editor = new BorderlessTextField("",9);
        editor.setBorder(null);
        return editor;
    
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("");
        }