- java.lang.Object
-
- javax.swing.tree.DefaultTreeCellEditor
-
- All Implemented Interfaces:
- ActionListener, EventListener, CellEditor, TreeSelectionListener, TreeCellEditor
public class DefaultTreeCellEditor extends Object implements ActionListener, TreeCellEditor, TreeSelectionListener
ATreeCellEditor
. You need to supply an instance ofDefaultTreeCellRenderer
so that the icons can be obtained. You can optionally supply aTreeCellEditor
that will be layed out according to the icon in theDefaultTreeCellRenderer
. If you do not supply aTreeCellEditor
, aTextField
will be used. Editing is started on a triple mouse click, or after a click, pause, click and a delay of 1200 miliseconds.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 seeXMLEncoder
.- See Also:
JTree
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description class
DefaultTreeCellEditor.DefaultTextField
TextField
used when no editor is supplied.class
DefaultTreeCellEditor.EditorContainer
Container responsible for placing theeditingComponent
.
-
Field Summary
Fields Modifier and Type Field and 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 theeditorComponent
.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 and Description DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer)
Constructs aDefaultTreeCellEditor
object for a JTree using the specified renderer and a default editor.DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor)
Constructs aDefaultTreeCellEditor
object for aJTree
using the specified renderer and the specified editor.
-
Method Summary
Methods Modifier and Type Method and Description void
actionPerformed(ActionEvent e)
Messaged when the timer fires, this will start the editing session.void
addCellEditorListener(CellEditorListener l)
Adds theCellEditorListener
.void
cancelCellEditing()
MessagescancelCellEditing
to therealEditor
and removes it from this instance.protected boolean
canEditImmediately(EventObject event)
Returns true ifevent
isnull
, or it is aMouseEvent
with a click count > 2 andinHitRegion
returns true.protected Container
createContainer()
Creates the container to manage placement ofeditingComponent
.protected TreeCellEditor
createTreeCellEditor()
This is invoked if aTreeCellEditor
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 theCellEditorListener
s 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 therealEditor
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 addedCellEditorListener
.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 therealEditor
for the return value.protected boolean
shouldStartEditingTimer(EventObject event)
Returns true ifevent
is aMouseEvent
and the click count is 1.protected void
startEditingTimer()
Starts the editing timer.boolean
stopCellEditing()
If therealEditor
will allow editing to stop, therealEditor
is removed and true is returned, otherwise false is returned.void
valueChanged(TreeSelectionEvent e)
ResetslastPath
.
-
-
-
Field Detail
-
realEditor
protected TreeCellEditor realEditor
Editor handling the editing.
-
renderer
protected DefaultTreeCellRenderer renderer
Renderer, used to get border and offsets from.
-
editingContainer
protected Container editingContainer
Editing container, will contain theeditorComponent
.
-
editingComponent
protected transient Component editingComponent
Component used in editing, obtained from theeditingContainer
.
-
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 overrideisCellEditable
.
-
offset
protected transient int offset
Used in editing. Indicates x position to placeeditingComponent
.
-
tree
protected transient JTree tree
JTree
instance listening too.
-
lastPath
protected transient TreePath lastPath
Last path that was selected.
-
timer
protected transient Timer timer
Used before starting the editing session.
-
lastRow
protected transient int lastRow
Row that was last passed intogetTreeCellEditorComponent
.
-
borderSelectionColor
protected Color borderSelectionColor
True if the border selection color should be drawn.
-
editingIcon
protected transient Icon editingIcon
Icon to use when editing.
-
font
protected Font font
Font to paint with,null
indicates font of renderer is to be used.
-
-
Constructor Detail
-
DefaultTreeCellEditor
public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer)
Constructs aDefaultTreeCellEditor
object for a JTree using the specified renderer and a default editor. (Use this constructor for normal editing.)- Parameters:
tree
- aJTree
objectrenderer
- aDefaultTreeCellRenderer
object
-
DefaultTreeCellEditor
public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor)
Constructs aDefaultTreeCellEditor
object for aJTree
using the specified renderer and the specified editor. (Use this constructor for specialized editing.)- Parameters:
tree
- aJTree
objectrenderer
- aDefaultTreeCellRenderer
objecteditor
- aTreeCellEditor
object
-
-
Method Detail
-
setBorderSelectionColor
public void setBorderSelectionColor(Color newColor)
Sets the color to use for the border.- Parameters:
newColor
- the new border color
-
getBorderSelectionColor
public Color getBorderSelectionColor()
Returns the color the border is drawn.- Returns:
- the border selection color
-
setFont
public void setFont(Font 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 instantied with. Ifnull
for an editor was passed in a default editor will be created that will pick up this font.- Parameters:
font
- the editingFont
- See Also:
getFont()
-
getFont
public Font getFont()
Gets the font used for editing.- Returns:
- the editing
Font
- See Also:
setFont(java.awt.Font)
-
getTreeCellEditorComponent
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
Configures the editor. Passed onto therealEditor
.- Specified by:
getTreeCellEditorComponent
in 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
public Object getCellEditorValue()
Returns the value currently being edited.- Specified by:
getCellEditorValue
in interfaceCellEditor
- Returns:
- the value currently being edited
-
isCellEditable
public boolean isCellEditable(EventObject event)
If therealEditor
returns true to this message,prepareForEditing
is messaged and true is returned.- Specified by:
isCellEditable
in 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:
CellEditor.shouldSelectCell(java.util.EventObject)
-
shouldSelectCell
public boolean shouldSelectCell(EventObject event)
Messages therealEditor
for the return value.- Specified by:
shouldSelectCell
in 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:
CellEditor.isCellEditable(java.util.EventObject)
-
stopCellEditing
public boolean stopCellEditing()
If therealEditor
will allow editing to stop, therealEditor
is removed and true is returned, otherwise false is returned.- Specified by:
stopCellEditing
in interfaceCellEditor
- Returns:
- true if editing was stopped; false otherwise
-
cancelCellEditing
public void cancelCellEditing()
MessagescancelCellEditing
to therealEditor
and removes it from this instance.- Specified by:
cancelCellEditing
in interfaceCellEditor
-
addCellEditorListener
public void addCellEditorListener(CellEditorListener l)
Adds theCellEditorListener
.- Specified by:
addCellEditorListener
in interfaceCellEditor
- Parameters:
l
- the listener to be added
-
removeCellEditorListener
public void removeCellEditorListener(CellEditorListener l)
Removes the previously addedCellEditorListener
.- Specified by:
removeCellEditorListener
in interfaceCellEditor
- Parameters:
l
- the listener to be removed
-
getCellEditorListeners
public CellEditorListener[] getCellEditorListeners()
Returns an array of all theCellEditorListener
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 e)
ResetslastPath
.- Specified by:
valueChanged
in interfaceTreeSelectionListener
- Parameters:
e
- the event that characterizes the change.
-
actionPerformed
public void actionPerformed(ActionEvent e)
Messaged when the timer fires, this will start the editing session.- Specified by:
actionPerformed
in interfaceActionListener
-
setTree
protected void setTree(JTree 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 event)
Returns true ifevent
is aMouseEvent
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 event)
Returns true ifevent
isnull
, or it is aMouseEvent
with a click count > 2 andinHitRegion
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 ifx
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 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)
-
prepareForEditing
protected void prepareForEditing()
Invoked just before editing is to start. Will add theeditingComponent
to theeditingContainer
.
-
createContainer
protected Container createContainer()
Creates the container to manage placement ofeditingComponent
.
-
createTreeCellEditor
protected TreeCellEditor createTreeCellEditor()
This is invoked if aTreeCellEditor
is not supplied in the constructor. It returns aTextField
editor.- Returns:
- a new
TextField
editor
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-javax/swing/tree/defaulttreecelleditor.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.