java.lang.Object
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassTextFieldused when no editor is supplied.classContainer responsible for placing theeditingComponent. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ColorTrue if the border selection color should be drawn.protected booleanAs of Java 2 platform v1.4 this field should no longer be used.protected ComponentComponent used in editing, obtained from theeditingContainer.protected ContainerEditing container, will contain theeditorComponent.protected IconIcon to use when editing.protected FontFont to paint with,nullindicates font of renderer is to be used.protected TreePathLast path that was selected.protected intRow that was last passed intogetTreeCellEditorComponent.protected intUsed in editing.protected TreeCellEditorEditor handling the editing.protected DefaultTreeCellRendererRenderer, used to get border and offsets from.protected TimerUsed before starting the editing session.protected JTreeJTreeinstance listening too. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) Constructs aDefaultTreeCellEditorobject for a JTree using the specified renderer and a default editor.DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor) Constructs aDefaultTreeCellEditorobject for aJTreeusing the specified renderer and the specified editor. -
Method Summary
Modifier and TypeMethodDescriptionvoidMessaged when the timer fires, this will start the editing session.voidAdds theCellEditorListener.voidMessagescancelCellEditingto therealEditorand removes it from this instance.protected booleancanEditImmediately(EventObject event) Returns true ifeventisnull, or it is aMouseEventwith a click count > 2 andinHitRegionreturns true.protected ContainerCreates the container to manage placement ofeditingComponent.protected TreeCellEditorThis is invoked if aTreeCellEditoris not supplied in the constructor.protected voiddetermineOffset(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) Determine the offset.Returns the color the border is drawn.Returns an array of all theCellEditorListeners added to this DefaultTreeCellEditor with addCellEditorListener().Returns the value currently being edited.getFont()Gets the font used for editing.getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) Configures the editor.protected booleaninHitRegion(int x, int y) Returns true if the passed in location is a valid mouse location to start editing from.booleanisCellEditable(EventObject event) If therealEditorreturns true to this message,prepareForEditingis messaged and true is returned.protected voidInvoked just before editing is to start.voidRemoves the previously addedCellEditorListener.voidsetBorderSelectionColor(Color newColor) Sets the color to use for the border.voidSets the font to edit with.protected voidSets the tree currently editing for.booleanshouldSelectCell(EventObject event) Messages therealEditorfor the return value.protected booleanReturns true ifeventis aMouseEventand the click count is 1.protected voidStarts the editing timer.booleanIf therealEditorwill allow editing to stop, therealEditoris removed and true is returned, otherwise false is returned.voidResetslastPath.
-
Field Details
-
realEditor
Editor handling the editing. -
renderer
Renderer, used to get border and offsets from. -
editingContainer
Editing container, will contain theeditorComponent. -
editingComponent
Component used in editing, obtained from theeditingContainer. -
canEdit
protected boolean canEditAs of Java 2 platform v1.4 this field should no longer be used. If you wish to provide similar behavior you should directly overrideisCellEditable. -
offset
protected transient int offsetUsed in editing. Indicates x position to placeeditingComponent. -
tree
JTreeinstance listening too. -
lastPath
Last path that was selected. -
timer
Used before starting the editing session. -
lastRow
protected transient int lastRowRow that was last passed intogetTreeCellEditorComponent. -
borderSelectionColor
True if the border selection color should be drawn. -
editingIcon
Icon to use when editing. -
font
Font to paint with,nullindicates font of renderer is to be used.
-
-
Constructor Details
-
DefaultTreeCellEditor
Constructs aDefaultTreeCellEditorobject for a JTree using the specified renderer and a default editor. (Use this constructor for normal editing.)- Parameters:
tree- aJTreeobjectrenderer- aDefaultTreeCellRendererobject
-
DefaultTreeCellEditor
Constructs aDefaultTreeCellEditorobject for aJTreeusing the specified renderer and the specified editor. (Use this constructor for specialized editing.)- Parameters:
tree- aJTreeobjectrenderer- aDefaultTreeCellRendererobjecteditor- aTreeCellEditorobject
-
-
Method Details
-
setBorderSelectionColor
Sets the color to use for the border.- Parameters:
newColor- the new border color
-
getBorderSelectionColor
Returns the color the border is drawn.- Returns:
- the border selection color
-
setFont
Sets the font to edit with.nullindicates the renderers font should be used. This will NOT override any font you have set in the editor the receiver was instantiated with. Ifnullfor an editor was passed in a default editor will be created that will pick up this font.- Parameters:
font- the editingFont- See Also:
-
getFont
Gets the font used for editing.- Returns:
- the editing
Font - See Also:
-
getTreeCellEditorComponent
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) Configures the editor. Passed onto therealEditor.- Specified by:
getTreeCellEditorComponentin interfaceTreeCellEditor- Parameters:
tree- the JTree that is asking the editor to edit; this parameter can be nullvalue- the value of the cell to be editedisSelected- true if the cell is to be rendered with selection highlightingexpanded- true if the node is expandedleaf- true if the node is a leaf noderow- the row index of the node being edited- Returns:
- the component for editing
-
getCellEditorValue
Returns the value currently being edited.- Specified by:
getCellEditorValuein interfaceCellEditor- Returns:
- the value currently being edited
-
isCellEditable
If therealEditorreturns true to this message,prepareForEditingis messaged and true is returned.- Specified by:
isCellEditablein interfaceCellEditor- 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:
-
shouldSelectCell
Messages therealEditorfor the return value.- Specified by:
shouldSelectCellin interfaceCellEditor- 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:
-
stopCellEditing
public boolean stopCellEditing()If therealEditorwill allow editing to stop, therealEditoris removed and true is returned, otherwise false is returned.- Specified by:
stopCellEditingin interfaceCellEditor- Returns:
- true if editing was stopped; false otherwise
-
cancelCellEditing
public void cancelCellEditing()MessagescancelCellEditingto therealEditorand removes it from this instance.- Specified by:
cancelCellEditingin interfaceCellEditor
-
addCellEditorListener
Adds theCellEditorListener.- Specified by:
addCellEditorListenerin interfaceCellEditor- Parameters:
l- the listener to be added
-
removeCellEditorListener
Removes the previously addedCellEditorListener.- Specified by:
removeCellEditorListenerin interfaceCellEditor- Parameters:
l- the listener to be removed
-
getCellEditorListeners
Returns an array of all theCellEditorListeners added to this DefaultTreeCellEditor with addCellEditorListener().- Returns:
- all of the
CellEditorListeners added or an empty array if no listeners have been added - Since:
- 1.4
-
valueChanged
ResetslastPath.- Specified by:
valueChangedin interfaceTreeSelectionListener- Parameters:
e- the event that characterizes the change.
-
actionPerformed
Messaged when the timer fires, this will start the editing session.- Specified by:
actionPerformedin interfaceActionListener- Parameters:
e- the event to be processed
-
setTree
Sets the tree currently editing for. This is needed to add a selection listener.- Parameters:
newTree- the new tree to be edited
-
shouldStartEditingTimer
Returns true ifeventis aMouseEventand the click count is 1.- Parameters:
event- the event being studied- Returns:
- whether
eventshould starts the editing timer
-
startEditingTimer
protected void startEditingTimer()Starts the editing timer. -
canEditImmediately
Returns true ifeventisnull, or it is aMouseEventwith a click count > 2 andinHitRegionreturns true.- Parameters:
event- the event being studied- Returns:
- whether editing can be started for the given
event
-
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 ifxis <= 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 pointy- the y-coordinate of the point- Returns:
- true if the passed in location is a valid mouse location
-
determineOffset
protected void determineOffset(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) Determine the offset.- Parameters:
tree- aJTreeobjectvalue- a valueisSelected- selection statusexpanded- expansion statusleaf- leaf statusrow- current row
-
prepareForEditing
protected void prepareForEditing()Invoked just before editing is to start. Will add theeditingComponentto theeditingContainer. -
createContainer
Creates the container to manage placement ofeditingComponent.- Returns:
- new Container object
-
createTreeCellEditor
This is invoked if aTreeCellEditoris not supplied in the constructor. It returns aTextFieldeditor.- Returns:
- a new
TextFieldeditor
-