FileDocCategorySizeDatePackage
DefaultCellEditor.javaAPI DocJava SE 6 API10040Tue Jun 10 00:26:36 BST 2008javax.swing

DefaultCellEditor

public class DefaultCellEditor extends AbstractCellEditor implements TreeCellEditor, TableCellEditor
The default editor for table and tree cells.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.52 11/17/05
author
Alan Chung
author
Philip Milne

Fields Summary
protected JComponent
editorComponent
The Swing component being edited.
protected EditorDelegate
delegate
The delegate class which handles all methods sent from the CellEditor.
protected int
clickCountToStart
An integer specifying the number of clicks needed to start editing. Even if clickCountToStart is defined as zero, it will not initiate until a click occurs.
Constructors Summary
public DefaultCellEditor(JTextField textField)
Constructs a DefaultCellEditor that uses a text field.

param
textField a JTextField object


//
//  Constructors
//

                       
        
        editorComponent = textField;
	this.clickCountToStart = 2;
        delegate = new EditorDelegate() {
            public void setValue(Object value) {
		textField.setText((value != null) ? value.toString() : "");
            }

	    public Object getCellEditorValue() {
		return textField.getText();
	    }
        };
	textField.addActionListener(delegate);
    
public DefaultCellEditor(JCheckBox checkBox)
Constructs a DefaultCellEditor object that uses a check box.

param
checkBox a JCheckBox object

        editorComponent = checkBox;
        delegate = new EditorDelegate() {
            public void setValue(Object value) { 
            	boolean selected = false; 
		if (value instanceof Boolean) {
		    selected = ((Boolean)value).booleanValue();
		}
		else if (value instanceof String) {
		    selected = value.equals("true");
		}
		checkBox.setSelected(selected);
            }

	    public Object getCellEditorValue() {
		return Boolean.valueOf(checkBox.isSelected());
	    }
        };
	checkBox.addActionListener(delegate);
        checkBox.setRequestFocusEnabled(false);
    
public DefaultCellEditor(JComboBox comboBox)
Constructs a DefaultCellEditor object that uses a combo box.

param
comboBox a JComboBox object

        editorComponent = comboBox;
	comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        delegate = new EditorDelegate() {
	    public void setValue(Object value) {
		comboBox.setSelectedItem(value);
            }

	    public Object getCellEditorValue() {
		return comboBox.getSelectedItem();
	    }
                
            public boolean shouldSelectCell(EventObject anEvent) { 
                if (anEvent instanceof MouseEvent) { 
                    MouseEvent e = (MouseEvent)anEvent;
                    return e.getID() != MouseEvent.MOUSE_DRAGGED;
                }
                return true;
            }
	    public boolean stopCellEditing() {
		if (comboBox.isEditable()) {
		    // Commit edited value.
		    comboBox.actionPerformed(new ActionEvent(
				     DefaultCellEditor.this, 0, ""));
		}
		return super.stopCellEditing();
	    }
        };
	comboBox.addActionListener(delegate);
    
Methods Summary
public voidcancelCellEditing()
Forwards the message from the CellEditor to the delegate.

see
EditorDelegate#cancelCellEditing

	delegate.cancelCellEditing();
    
public java.lang.ObjectgetCellEditorValue()
Forwards the message from the CellEditor to the delegate.

see
EditorDelegate#getCellEditorValue

        return delegate.getCellEditorValue();
    
public intgetClickCountToStart()
Returns the number of clicks needed to start editing.

return
the number of clicks needed to start editing

	return clickCountToStart;
    
public java.awt.ComponentgetComponent()
Returns a reference to the editor component.

return
the editor Component

	return editorComponent;
    
public java.awt.ComponentgetTableCellEditorComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, int row, int column)
Implements the TableCellEditor interface.

        delegate.setValue(value);
	return editorComponent;
    
public java.awt.ComponentgetTreeCellEditorComponent(javax.swing.JTree tree, java.lang.Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
Implements the TreeCellEditor interface.

	String         stringValue = tree.convertValueToText(value, isSelected,
					    expanded, leaf, row, false);

	delegate.setValue(stringValue);
	return editorComponent;
    
public booleanisCellEditable(java.util.EventObject anEvent)
Forwards the message from the CellEditor to the delegate.

see
EditorDelegate#isCellEditable(EventObject)

 
	return delegate.isCellEditable(anEvent); 
    
public voidsetClickCountToStart(int count)
Specifies the number of clicks needed to start editing.

param
count an int specifying the number of clicks needed to start editing
see
#getClickCountToStart

	clickCountToStart = count;
    
public booleanshouldSelectCell(java.util.EventObject anEvent)
Forwards the message from the CellEditor to the delegate.

see
EditorDelegate#shouldSelectCell(EventObject)

 
	return delegate.shouldSelectCell(anEvent); 
    
public booleanstopCellEditing()
Forwards the message from the CellEditor to the delegate.

see
EditorDelegate#stopCellEditing

	return delegate.stopCellEditing();