DefaultTreeCellEditor (Java Platform SE 8 ) (original) (raw)
- javax.swing.tree.DefaultTreeCellEditor
All Implemented Interfaces:
ActionListener, EventListener, CellEditor, TreeSelectionListener, TreeCellEditor
public class DefaultTreeCellEditor
extends Object
implements ActionListener, TreeCellEditor, TreeSelectionListener
A TreeCellEditor
. You need to supply an instance of DefaultTreeCellRenderer
so that the icons can be obtained. You can optionally supply a TreeCellEditor
that will be layed out according to the icon in the DefaultTreeCellRenderer
. If you do not supply a TreeCellEditor
, a TextField
will be used. Editing is started on a triple mouse click, or after a click, pause, click and a delay of 1200 milliseconds.
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 JavaBeans™ has been added to the java.beans
package. Please see XMLEncoder.
See Also:
JTree
Nested Class Summary
Nested Classes
Modifier and Type Class Description class DefaultTreeCellEditor.DefaultTextField TextField used when no editor is supplied. class DefaultTreeCellEditor.EditorContainer Container responsible for placing the editingComponent. Field Summary
Fields
Modifier and Type Field Description protected Color borderSelectionColor True if the border selection color should be drawn. protected boolean canEdit As of Java 2 platform v1.4 this field should no longer be used. protected Component editingComponent Component used in editing, obtained from theeditingContainer. protected Container editingContainer Editing container, will contain the editorComponent. protected Icon editingIcon Icon to use when editing. protected Font font Font to paint with, null indicates font of renderer is to be used. protected TreePath lastPath Last path that was selected. protected int lastRow Row that was last passed intogetTreeCellEditorComponent. protected int offset Used in editing. protected TreeCellEditor realEditor Editor handling the editing. protected DefaultTreeCellRenderer renderer Renderer, used to get border and offsets from. protected Timer timer Used before starting the editing session. protected JTree tree JTree instance listening too. Constructor Summary
Constructors
Constructor Description DefaultTreeCellEditor(JTree tree,DefaultTreeCellRenderer renderer) Constructs a DefaultTreeCellEditor object for a JTree using the specified renderer and a default editor. DefaultTreeCellEditor(JTree tree,DefaultTreeCellRenderer renderer,TreeCellEditor editor) Constructs a DefaultTreeCellEditor object for a JTree using the specified renderer and the specified editor. Method Summary
All Methods Instance Methods Concrete Methods
Modifier and Type Method Description void actionPerformed(ActionEvent e) Messaged when the timer fires, this will start the editing session. void addCellEditorListener(CellEditorListener l) Adds the CellEditorListener. void cancelCellEditing() Messages cancelCellEditing to therealEditor and removes it from this instance. protected boolean canEditImmediately(EventObject event) Returns true if event is null, or it is a MouseEvent with a click count > 2 and inHitRegion returns true. protected Container createContainer() Creates the container to manage placement ofeditingComponent. protected TreeCellEditor createTreeCellEditor() This is invoked if a TreeCellEditor is not supplied in the constructor. protected void determineOffset(JTree tree,Object value, boolean isSelected, boolean expanded, boolean leaf, int row) Color getBorderSelectionColor() Returns the color the border is drawn. CellEditorListener[] getCellEditorListeners() Returns an array of all the CellEditorListeners added to this DefaultTreeCellEditor with addCellEditorListener(). Object getCellEditorValue() Returns the value currently being edited. Font getFont() Gets the font used for editing. Component getTreeCellEditorComponent(JTree tree,Object value, boolean isSelected, boolean expanded, boolean leaf, int row) Configures the editor. protected boolean inHitRegion(int x, int y) Returns true if the passed in location is a valid mouse location to start editing from. boolean isCellEditable(EventObject event) If the realEditor returns true to this message, prepareForEditing is messaged and true is returned. protected void prepareForEditing() Invoked just before editing is to start. void removeCellEditorListener(CellEditorListener l) Removes the previously added CellEditorListener. void setBorderSelectionColor(Color newColor) Sets the color to use for the border. void setFont(Font font) Sets the font to edit with. protected void setTree(JTree newTree) Sets the tree currently editing for. boolean shouldSelectCell(EventObject event) Messages the realEditor for the return value. protected boolean shouldStartEditingTimer(EventObject event) Returns true if event is a MouseEvent and the click count is 1. protected void startEditingTimer() Starts the editing timer. boolean stopCellEditing() If the realEditor will allow editing to stop, the realEditor is removed and true is returned, otherwise false is returned. void valueChanged(TreeSelectionEvent e) Resets lastPath. * ### Methods inherited from class java.lang.[Object](../../../java/lang/Object.html "class in java.lang") `[clone](../../../java/lang/Object.html#clone--), [equals](../../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../../java/lang/Object.html#finalize--), [getClass](../../../java/lang/Object.html#getClass--), [hashCode](../../../java/lang/Object.html#hashCode--), [notify](../../../java/lang/Object.html#notify--), [notifyAll](../../../java/lang/Object.html#notifyAll--), [toString](../../../java/lang/Object.html#toString--), [wait](../../../java/lang/Object.html#wait--), [wait](../../../java/lang/Object.html#wait-long-), [wait](../../../java/lang/Object.html#wait-long-int-)`
Field Detail
* #### realEditor protected [TreeCellEditor](../../../javax/swing/tree/TreeCellEditor.html "interface in javax.swing.tree") realEditor Editor handling the editing. * #### renderer protected [DefaultTreeCellRenderer](../../../javax/swing/tree/DefaultTreeCellRenderer.html "class in javax.swing.tree") renderer Renderer, used to get border and offsets from. * #### editingContainer protected [Container](../../../java/awt/Container.html "class in java.awt") editingContainer Editing container, will contain the `editorComponent`. * #### editingComponent protected transient [Component](../../../java/awt/Component.html "class in java.awt") editingComponent Component used in editing, obtained from the`editingContainer`. * #### canEdit protected boolean canEdit As of Java 2 platform v1.4 this field should no longer be used. If you wish to provide similar behavior you should directly override`isCellEditable`. * #### offset protected transient int offset Used in editing. Indicates x position to place`editingComponent`. * #### tree protected transient [JTree](../../../javax/swing/JTree.html "class in javax.swing") tree `JTree` instance listening too. * #### lastPath protected transient [TreePath](../../../javax/swing/tree/TreePath.html "class in javax.swing.tree") lastPath Last path that was selected. * #### timer protected transient [Timer](../../../javax/swing/Timer.html "class in javax.swing") timer Used before starting the editing session. * #### lastRow protected transient int lastRow Row that was last passed into`getTreeCellEditorComponent`. * #### borderSelectionColor protected [Color](../../../java/awt/Color.html "class in java.awt") borderSelectionColor True if the border selection color should be drawn. * #### editingIcon protected transient [Icon](../../../javax/swing/Icon.html "interface in javax.swing") editingIcon Icon to use when editing. * #### font protected [Font](../../../java/awt/Font.html "class in java.awt") font Font to paint with, `null` indicates font of renderer is to be used.
Constructor Detail
* #### DefaultTreeCellEditor public DefaultTreeCellEditor([JTree](../../../javax/swing/JTree.html "class in javax.swing") tree, [DefaultTreeCellRenderer](../../../javax/swing/tree/DefaultTreeCellRenderer.html "class in javax.swing.tree") renderer) Constructs a `DefaultTreeCellEditor` object for a JTree using the specified renderer and a default editor. (Use this constructor for normal editing.) Parameters: `tree` \- a `JTree` object `renderer` \- a `DefaultTreeCellRenderer` object * #### DefaultTreeCellEditor public DefaultTreeCellEditor([JTree](../../../javax/swing/JTree.html "class in javax.swing") tree, [DefaultTreeCellRenderer](../../../javax/swing/tree/DefaultTreeCellRenderer.html "class in javax.swing.tree") renderer, [TreeCellEditor](../../../javax/swing/tree/TreeCellEditor.html "interface in javax.swing.tree") editor) Constructs a `DefaultTreeCellEditor` object for a `JTree` using the specified renderer and the specified editor. (Use this constructor for specialized editing.) Parameters: `tree` \- a `JTree` object `renderer` \- a `DefaultTreeCellRenderer` object `editor` \- a `TreeCellEditor` object
Method Detail
* #### setBorderSelectionColor public void setBorderSelectionColor([Color](../../../java/awt/Color.html "class in java.awt") newColor) Sets the color to use for the border. Parameters: `newColor` \- the new border color * #### getBorderSelectionColor public [Color](../../../java/awt/Color.html "class in java.awt") getBorderSelectionColor() Returns the color the border is drawn. Returns: the border selection color * #### setFont public void setFont([Font](../../../java/awt/Font.html "class in java.awt") font) Sets the font to edit with. `null` indicates the renderers font should be used. This will NOT override any font you have set in the editor the receiver was instantiated with. If `null` for an editor was passed in a default editor will be created that will pick up this font. Parameters: `font` \- the editing `Font` See Also: [getFont()](../../../javax/swing/tree/DefaultTreeCellEditor.html#getFont--) * #### getFont public [Font](../../../java/awt/Font.html "class in java.awt") getFont() Gets the font used for editing. Returns: the editing `Font` See Also: [setFont(java.awt.Font)](../../../javax/swing/tree/DefaultTreeCellEditor.html#setFont-java.awt.Font-) * #### getTreeCellEditorComponent public [Component](../../../java/awt/Component.html "class in java.awt") getTreeCellEditorComponent([JTree](../../../javax/swing/JTree.html "class in javax.swing") tree, [Object](../../../java/lang/Object.html "class in java.lang") value, boolean isSelected, boolean expanded, boolean leaf, int row) Configures the editor. Passed onto the `realEditor`. Specified by: `[getTreeCellEditorComponent](../../../javax/swing/tree/TreeCellEditor.html#getTreeCellEditorComponent-javax.swing.JTree-java.lang.Object-boolean-boolean-boolean-int-)` in interface `[TreeCellEditor](../../../javax/swing/tree/TreeCellEditor.html "interface in javax.swing.tree")` Parameters: `tree` \- the JTree that is asking the editor to edit; this parameter can be null `value` \- the value of the cell to be edited `isSelected` \- true if the cell is to be rendered with selection highlighting `expanded` \- true if the node is expanded `leaf` \- true if the node is a leaf node `row` \- the row index of the node being edited Returns: the component for editing * #### getCellEditorValue public [Object](../../../java/lang/Object.html "class in java.lang") getCellEditorValue() Returns the value currently being edited. Specified by: `[getCellEditorValue](../../../javax/swing/CellEditor.html#getCellEditorValue--)` in interface `[CellEditor](../../../javax/swing/CellEditor.html "interface in javax.swing")` Returns: the value currently being edited * #### isCellEditable public boolean isCellEditable([EventObject](../../../java/util/EventObject.html "class in java.util") event) If the `realEditor` returns true to this message, `prepareForEditing` is messaged and true is returned. Specified by: `[isCellEditable](../../../javax/swing/CellEditor.html#isCellEditable-java.util.EventObject-)` in interface `[CellEditor](../../../javax/swing/CellEditor.html "interface in javax.swing")` Parameters: `event` \- the event the editor should use to consider whether to begin editing or not Returns: true if editing can be started See Also: [CellEditor.shouldSelectCell(java.util.EventObject)](../../../javax/swing/CellEditor.html#shouldSelectCell-java.util.EventObject-) * #### shouldSelectCell public boolean shouldSelectCell([EventObject](../../../java/util/EventObject.html "class in java.util") event) Messages the `realEditor` for the return value. Specified by: `[shouldSelectCell](../../../javax/swing/CellEditor.html#shouldSelectCell-java.util.EventObject-)` in interface `[CellEditor](../../../javax/swing/CellEditor.html "interface in javax.swing")` Parameters: `event` \- the event the editor should use to start editing Returns: true if the editor would like the editing cell to be selected; otherwise returns false See Also: [CellEditor.isCellEditable(java.util.EventObject)](../../../javax/swing/CellEditor.html#isCellEditable-java.util.EventObject-) * #### stopCellEditing public boolean stopCellEditing() If the `realEditor` will allow editing to stop, the `realEditor` is removed and true is returned, otherwise false is returned. Specified by: `[stopCellEditing](../../../javax/swing/CellEditor.html#stopCellEditing--)` in interface `[CellEditor](../../../javax/swing/CellEditor.html "interface in javax.swing")` Returns: true if editing was stopped; false otherwise * #### cancelCellEditing public void cancelCellEditing() Messages `cancelCellEditing` to the`realEditor` and removes it from this instance. Specified by: `[cancelCellEditing](../../../javax/swing/CellEditor.html#cancelCellEditing--)` in interface `[CellEditor](../../../javax/swing/CellEditor.html "interface in javax.swing")` * #### addCellEditorListener public void addCellEditorListener([CellEditorListener](../../../javax/swing/event/CellEditorListener.html "interface in javax.swing.event") l) Adds the `CellEditorListener`. Specified by: `[addCellEditorListener](../../../javax/swing/CellEditor.html#addCellEditorListener-javax.swing.event.CellEditorListener-)` in interface `[CellEditor](../../../javax/swing/CellEditor.html "interface in javax.swing")` Parameters: `l` \- the listener to be added * #### removeCellEditorListener public void removeCellEditorListener([CellEditorListener](../../../javax/swing/event/CellEditorListener.html "interface in javax.swing.event") l) Removes the previously added `CellEditorListener`. Specified by: `[removeCellEditorListener](../../../javax/swing/CellEditor.html#removeCellEditorListener-javax.swing.event.CellEditorListener-)` in interface `[CellEditor](../../../javax/swing/CellEditor.html "interface in javax.swing")` Parameters: `l` \- the listener to be removed * #### getCellEditorListeners public [CellEditorListener](../../../javax/swing/event/CellEditorListener.html "interface in javax.swing.event")[] getCellEditorListeners() Returns an array of all the `CellEditorListener`s added to this DefaultTreeCellEditor with addCellEditorListener(). Returns: all of the `CellEditorListener`s added or an empty array if no listeners have been added Since: 1.4 * #### valueChanged public void valueChanged([TreeSelectionEvent](../../../javax/swing/event/TreeSelectionEvent.html "class in javax.swing.event") e) Resets `lastPath`. Specified by: `[valueChanged](../../../javax/swing/event/TreeSelectionListener.html#valueChanged-javax.swing.event.TreeSelectionEvent-)` in interface `[TreeSelectionListener](../../../javax/swing/event/TreeSelectionListener.html "interface in javax.swing.event")` Parameters: `e` \- the event that characterizes the change. * #### actionPerformed public void actionPerformed([ActionEvent](../../../java/awt/event/ActionEvent.html "class in java.awt.event") e) Messaged when the timer fires, this will start the editing session. Specified by: `[actionPerformed](../../../java/awt/event/ActionListener.html#actionPerformed-java.awt.event.ActionEvent-)` in interface `[ActionListener](../../../java/awt/event/ActionListener.html "interface in java.awt.event")` * #### setTree protected void setTree([JTree](../../../javax/swing/JTree.html "class in javax.swing") newTree) Sets the tree currently editing for. This is needed to add a selection listener. Parameters: `newTree` \- the new tree to be edited * #### shouldStartEditingTimer protected boolean shouldStartEditingTimer([EventObject](../../../java/util/EventObject.html "class in java.util") event) Returns true if `event` is a `MouseEvent` and the click count is 1. Parameters: `event` \- the event being studied * #### startEditingTimer protected void startEditingTimer() Starts the editing timer. * #### canEditImmediately protected boolean canEditImmediately([EventObject](../../../java/util/EventObject.html "class in java.util") event) Returns true if `event` is `null`, or it is a `MouseEvent` with a click count > 2 and `inHitRegion` returns true. Parameters: `event` \- the event being studied * #### inHitRegion protected boolean inHitRegion(int x, int y) Returns true if the passed in location is a valid mouse location to start editing from. This is implemented to return false if`x` is <= the width of the icon and icon gap displayed by the renderer. In other words this returns true if the user clicks over the text part displayed by the renderer, and false otherwise. Parameters: `x` \- the x-coordinate of the point `y` \- the y-coordinate of the point Returns: true if the passed in location is a valid mouse location * #### determineOffset protected void determineOffset([JTree](../../../javax/swing/JTree.html "class in javax.swing") tree, [Object](../../../java/lang/Object.html "class in java.lang") value, boolean isSelected, boolean expanded, boolean leaf, int row) * #### prepareForEditing protected void prepareForEditing() Invoked just before editing is to start. Will add the`editingComponent` to the`editingContainer`. * #### createContainer protected [Container](../../../java/awt/Container.html "class in java.awt") createContainer() Creates the container to manage placement of`editingComponent`. * #### createTreeCellEditor protected [TreeCellEditor](../../../javax/swing/tree/TreeCellEditor.html "interface in javax.swing.tree") createTreeCellEditor() This is invoked if a `TreeCellEditor` is not supplied in the constructor. It returns a `TextField` editor. Returns: a new `TextField` editor
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.