A specific node in a tree can be identified either by a
TreePath
(an object
that encapsulates a node and all of its ancestors), or by its
display row, where each row in the display area displays one node.
An expanded node is a non-leaf node (as identified by
TreeModel.isLeaf(node)
returning false) that will displays
its children when all its ancestors are expanded.
A collapsed
node is one which hides them. A hidden node is one which is
under a collapsed ancestor. All of a viewable nodes parents
are expanded, but may or may not be displayed. A displayed node
is both viewable and in the display area, where it can be seen.
JTree
methods use "visible" to mean "displayed":
isRootVisible()
setRootVisible()
scrollPathToVisible()
scrollRowToVisible()
getVisibleRowCount()
setVisibleRowCount()
JTree
methods use "visible" to mean
"viewable" (under an expanded parent):
isVisible()
makeVisible()
TreeSelectionListener
interface and add the instance
using the method addTreeSelectionListener
.
valueChanged
will be invoked when the
selection changes, that is if the user clicks twice on the same
node valueChanged
will only be invoked once.
If you are interested in detecting either double-click events or when a user clicks on a node, regardless of whether or not it was selected, we recommend you do the following:
final JTree tree = ...; MouseListener ml = new MouseAdapter() { public void mousePressed(MouseEvent e) { int selRow = tree.getRowForLocation(e.getX(), e.getY()); TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); if(selRow != -1) { if(e.getClickCount() == 1) { mySingleClick(selRow, selPath); } else if(e.getClickCount() == 2) { myDoubleClick(selRow, selPath); } } } }; tree.addMouseListener(ml);NOTE: This example obtains both the path and row, but you only need to get the one you're interested in.
To use JTree
to display compound nodes
(for example, nodes containing both
a graphic icon and text), subclass TreeCellRenderer
and use
setCellRenderer
to tell the tree to use it. To edit such nodes,
subclass TreeCellEditor
and use setCellEditor
.
Like all JComponent
classes, you can use InputMap
and
ActionMap
to associate an Action
object with a KeyStroke
and execute the action under specified conditions.
Warning
Swing is not thread safe. For more information see Swing's Threading Policy.
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 java.
.
Modifier and Type | Class and Description |
---|---|
protected class | JTree.
This class implements accessibility support for the
|
public static class | JTree.
A subclass of |
public static class | JTree.
|
protected static class | JTree.
|
protected class | JTree.
Listens to the model and updates the |
protected class | JTree.
Handles creating a new |
private class |
Modifier and Type | Field and Description |
---|---|
public static final String | ANCHOR_SELECTION_PATH_PROPERTY
Bound property name for anchor selection path. |
private TreePath | anchorPath
Anchor path. |
public static final String | CELL_EDITOR_PROPERTY
Bound property name for |
public static final String | CELL_RENDERER_PROPERTY
Bound property name for |
protected transient TreeCellEditor | cellEditor
Editor for the entries. |
protected transient TreeCellRenderer | cellRenderer
The cell used to draw nodes. |
private boolean | dragEnabled
If true, mouse presses on selections initiate a drag operation. |
private transient JTree. | dropLocation
The drop location. |
private DropMode | dropMode
The drop mode for this component. |
private JTree. | dropTimer
A timer to expand nodes during drop. |
protected boolean | editable
Is the tree editable? |
public static final String | EDITABLE_PROPERTY
Bound property name for |
private transient Stack | expandedStack
Used when |
private transient Hashtable | expandedState
Maps from |
private int | expandRow
The row to expand during DnD. |
public static final String | EXPANDS_SELECTED_PATHS_PROPERTY
Bound property name for expands selected paths property |
private boolean | expandsSelectedPaths
True if paths in the selection should be expanded. |
public static final String | INVOKES_STOP_CELL_EDITING_PROPERTY
Bound property name for |
protected boolean | invokesStopCellEditing
If true, when editing is to be stopped by way of selection changing,
data in tree changing or other means |
public static final String | LARGE_MODEL_PROPERTY
Bound property name for |
protected boolean | largeModel
Is this tree a large model? |
public static final String | LEAD_SELECTION_PATH_PROPERTY
Bound property name for |
private TreePath | leadPath
Lead selection path, may not be |
public static final String | ROOT_VISIBLE_PROPERTY
Bound property name for |
protected boolean | rootVisible
True if the root node is displayed, false if its children are the highest visible nodes. |
public static final String | ROW_HEIGHT_PROPERTY
Bound property name for |
protected int | rowHeight
Height to use for each display row. |
private boolean | |
public static final String | SCROLLS_ON_EXPAND_PROPERTY
Bound property name for |
protected boolean | scrollsOnExpand
If true, when a node is expanded, as many of the descendants are scrolled to be visible. |
private boolean | |
public static final String | SELECTION_MODEL_PROPERTY
Bound property name for selectionModel. |
protected transient TreeSelectionModel | selectionModel
Models the set of selected nodes in this tree. |
protected transient JTree. | selectionRedirector
Creates a new event and passed it off the
|
private boolean | settingUI
This is set to true for the life of the |
public static final String | SHOWS_ROOT_HANDLES_PROPERTY
Bound property name for |
protected boolean | showsRootHandles
True if handles are displayed at the topmost level of the tree. |
private boolean | |
private static int | TEMP_STACK_SIZE
Max number of stacks to keep around. |
public static final String | TOGGLE_CLICK_COUNT_PROPERTY
Bound property name for |
protected int | toggleClickCount
Number of mouse clicks before a node is expanded. |
public static final String | TREE_MODEL_PROPERTY
Bound property name for |
protected transient TreeModel | treeModel
The model that defines the tree displayed by this object. |
protected transient TreeModelListener | treeModelListener
Updates the |
private static final String | |
private transient TreeExpansionListener | uiTreeExpansionListener
When |
private transient boolean | updateInProgress
Flag to indicate UI update is in progress |
public static final String | VISIBLE_ROW_COUNT_PROPERTY
Bound property name for |
protected int | visibleRowCount
Number of rows to make visible at one time. |
Access | Constructor and Description |
---|---|
public | |
public | |
public | |
public | |
public | |
public | JTree(TreeNode
a root, boolean TreeNode objectif false, any node without children is a
leaf node; if true, only nodes that do not allow
children are leaf nodes asksAllowsChildren)Returns a |
public |
Modifier and Type | Method and Description |
---|---|
public void | addSelectionInterval(int
the first index in the range to add to the selection index0, int the last index in the range to add to the selection index1)Adds the specified rows (inclusive) to the selection. |
public void | addSelectionPath(TreePath
the path)TreePath to addAdds the node identified by the specified |
public void | addSelectionPaths(TreePath[]
an array of paths)TreePath objects that specifies
the nodes to addAdds each path in the array of paths to the current selection. |
public void | addSelectionRow(int
an integer specifying the row of the node to add,
where 0 is the first row in the display row)Adds the path at the specified row to the current selection. |
public void | addSelectionRows(int[]
an array of ints specifying the rows to add,
where 0 indicates the first row in the display rows)Adds the paths at each of the specified rows to the current selection. |
public void | addTreeExpansionListener(TreeExpansionListener
a TreeExpansionListener that will be notified when
a tree node is expanded or collapsed (a "negative
expansion") tel)Adds a listener for |
public void | addTreeSelectionListener(TreeSelectionListener
the tsl)TreeSelectionListener that will be notified
when a node is selected or deselected (a "negative
selection")Adds a listener for |
public void | addTreeWillExpandListener(TreeWillExpandListener
a tel)TreeWillExpandListener that will be notified
when a tree node will be expanded or collapsed (a "negative
expansion")Adds a listener for |
private void | |
public void | |
private static void | |
private static void | |
public void | |
protected void | |
public void | collapsePath(TreePath
the path)TreePath identifying a nodeEnsures that the node identified by the specified path is collapsed and viewable. |
public void | collapseRow(int
an integer specifying a display row, where 0 is the
first row in the display row)Ensures that the node in the specified row is collapsed. |
public String | Returns: theString representation of the node's valuethe value, boolean Object to convert to texttrue if the node is selected selected, boolean true if the node is expanded expanded, boolean true if the node is a leaf node leaf, int an integer specifying the node's display row, where 0 is
the first row in the display row, boolean true if the node has the focus hasFocus)Called by the renderers to convert the specified value to text. |
protected static TreeModel | Returns: aTreeModel wrapping the specified objectthe value)Object used as the foundation for
the TreeModel Returns a |
protected TreeModelListener | Returns: the instance ofTreeModelHandler Creates and returns an instance of |
pack-priv void | dndDone()
Overrides javax. |
pack-priv JTree. | Returns: the drop location, ornull the point to calculate a drop location for p)Overrides javax. |
public void | expandPath(TreePath
the path)TreePath identifying a nodeEnsures that the node identified by the specified path is expanded and viewable. |
private void | |
public void | expandRow(int
an integer specifying a display row, where 0 is the
first row in the display row)Ensures that the node in the specified row is expanded and viewable. |
public void | fireTreeCollapsed(TreePath
the path)TreePath indicating the node that was
collapsedNotifies all listeners that have registered interest for notification on this event type. |
public void | fireTreeExpanded(TreePath
the path)TreePath indicating the node that was
expandedNotifies all listeners that have registered interest for notification on this event type. |
public void | fireTreeWillCollapse(TreePath
the path)TreePath indicating the node that was
expandedNotifies all listeners that have registered interest for notification on this event type. |
public void | fireTreeWillExpand(TreePath
the path)TreePath indicating the node that was
expandedNotifies all listeners that have registered interest for notification on this event type. |
protected void | fireValueChanged(TreeSelectionEvent
the e)TreeSelectionEvent to be fired;
generated by the
TreeSelectionModel
when a node is selected or deselectedNotifies all listeners that have registered interest for notification on this event type. |
public AccessibleContext | Returns: an AccessibleJTree that serves as the AccessibleContext of this JTreeOverrides java. Implements javax. |
public TreePath | Returns: path identified as the anchorReturns the path identified as the anchor. |
private Object | getArchivableExpandedState()
Returns an object that can be archived indicating what nodes are expanded and what aren't. |
public TreeCellEditor | Returns: theTreeCellEditor in use,
or null if the tree cannot be editedReturns the editor used to edit entries in the tree. |
public TreeCellRenderer | Returns: theTreeCellRenderer that is rendering each cellReturns the current |
public TreePath | Returns: theTreePath for the node closest to that location,
null if nothing is viewable or there is no modelan integer giving the number of pixels horizontally from
the left edge of the display area, minus any left margin x, int an integer giving the number of pixels vertically from
the top of the display area, minus any top margin y)Returns the path to the node that is closest to x,y. |
public int | Returns: the row closest to the location, -1 if nothing is viewable or there is no modelan integer giving the number of pixels horizontally from
the left edge of the display area, minus any left margin x, int an integer giving the number of pixels vertically from
the top of the display area, minus any top margin y)Returns the row to the node that is closest to x,y. |
protected static TreeModel | |
private TreePath[] | getDescendantSelectedPaths(TreePath path, boolean includePath)
Returns an array of paths in the selection that are descendants of
|
protected Enumeration | Returns: theEnumeration of TreePaths a path parent)Returns an |
public boolean | Returns: the value of thedragEnabled propertyReturns whether or not automatic drag handling is enabled. |
public final JTree. | Returns: the drop locationReturns the location that this component should visually indicate
as the drop location during a DnD operation over the component,
or |
public final DropMode | |
public TreePath | Returns: theTreePath for the node being editedReturns the path to the element that is currently being edited. |
public Enumeration | Returns: anEnumeration of the descendents of
parent , or null if
parent is not currently expandedthe path which is to be examined parent)Returns an |
public boolean | Returns: true if selection changes result in the parent path being expandedReturns the |
public boolean | Returns: the indicator that tells what happens when editing is interruptedReturns the indicator that tells what happens when editing is interrupted. |
public Object | Returns: the last path component of the selected path, ornull if nothing is selectedReturns the last path component of the selected path. |
public TreePath | |
public int | Returns: an integer giving the row index of the lead path, where 0 is the first row in the display; or -1 ifleadPath is null Returns the row index corresponding to the lead path. |
public int | |
public int | |
public TreeModel | Returns: theTreeModel that is providing the dataReturns the |
private int[] | getModelIndexsForPath(TreePath path)
Returns an array of integers specifying the indices of the
components in the |
public TreePath | Returns: the TreePath of the next tree element that starts with the prefix; otherwise nullthe string to test for a match prefix, int the row for starting the search startingRow, Position.the search direction, either
Position.Bias.Forward or Position.Bias.Backward. biasReturns the TreePath to the next tree element that begins with a prefix. |
protected TreePath[] | Returns: the paths (inclusive) between the specified row indicesthe first index in the range index0, int the last index in the range index1)Returns the paths (inclusive) between the specified rows. |
public Rectangle | Returns: theRectangle the node is drawn in,
or null the path)TreePath identifying the nodeReturns the |
private TreePath | getPathForIndexs(int[] indices)
Returns a |
public TreePath | Returns: theTreePath for the node at that locationan integer giving the number of pixels horizontally from
the left edge of the display area, minus any left margin x, int an integer giving the number of pixels vertically from
the top of the display area, minus any top margin y)Returns the path for the node at the specified location. |
public TreePath | Returns: theTreePath to the specified node,
null if row < 0
or row >= getRowCount() an integer specifying a row row)Returns the path for the specified row. |
public Dimension | Returns: aDimension object containing the preferred sizeImplements javax. JTree .
|
public Rectangle | Returns: theRectangle the node is drawn inthe row to be drawn, where 0 is the first row in the
display row)Returns the |
public int | |
public int | Returns: the row corresponding to the location, or -1 if the location is not within the bounds of a displayed cellan integer giving the number of pixels horizontally from
the left edge of the display area, minus any left margin x, int an integer giving the number of pixels vertically from
the top of the display area, minus any top margin y)Returns the row for the specified location. |
public int | Returns: an integer specifying the display row, where 0 is the first row in the display, or -1 if any of the elements in path are hidden under a collapsed parent.the path)TreePath identifying a nodeReturns the row that displays the node identified by the specified path. |
public int | |
public int | Returns: the "block" increment for scrolling in the specified directionthe view area visible within the viewport visibleRect, int either orientation, int SwingConstants.VERTICAL
or SwingConstants.HORIZONTAL less than zero to scroll up/left,
greater than zero for down/right. direction)Implements javax. visibleRect , based on orientation .
|
public boolean | Returns: whether the tree should track the height of the viewportImplements javax. |
public boolean | Returns: whether the tree should track the width of the viewportImplements javax. |
public int | Returns: the "unit" increment for scrolling in the specified directionthe view area visible within the viewport visibleRect, int either orientation, int SwingConstants.VERTICAL
or SwingConstants.HORIZONTAL less than zero to scroll up/left,
greater than zero for down/right direction)Implements javax. |
public boolean | Returns: the value of thescrollsOnExpand propertyReturns the value of the |
public int | |
public TreeSelectionModel | |
public TreePath | Returns: theTreePath for the first selected node,
or null if nothing is currently selectedReturns the path to the first selected node. |
public TreePath[] | Returns: an array ofTreePath objects indicating the selected
nodes, or null if nothing is currently selectedReturns the paths of all selected values. |
public int[] | Returns: an array of integers that identifies all currently selected rows where 0 is the first row in the displayReturns all of the currently selected rows. |
public boolean | Returns: the value of theshowsRootHandles propertyReturns the value of the |
public int | Returns: number of mouse clicks before node is expandedReturns the number of mouse clicks needed to expand or close a node. |
public String | Returns: a string containing the tooltip ornull
if event is nullthe event)MouseEvent that initiated the
ToolTip displayOverrides javax. JComponent 's getToolTipText
method in order to allow
renderer's tips to be used if it has text set.
|
public TreeExpansionListener[] | Returns: all of theTreeExpansionListener s added or an empty
array if no listeners have been addedReturns an array of all the |
public TreeSelectionListener[] | Returns: all of theTreeSelectionListener s added or an empty
array if no listeners have been addedReturns an array of all the |
public TreeWillExpandListener[] | Returns: all of theTreeWillExpandListener s added or an empty
array if no listeners have been addedReturns an array of all the |
public TreeUI | Returns: theTreeUI object that renders this componentOverrides javax. |
public String | Returns: the string "TreeUI"Overrides javax. |
public int | Returns: the number of rows displayedReturns the number of rows that are displayed in the display area. |
public boolean | Returns: true if thepath has ever been expandeda path)TreePath identifying a nodeReturns true if the node identified by the path has ever been expanded. |
public boolean | Returns: true if any of the nodes in the node's path are collapsed, false if all nodes in the path are expandedthe path)TreePath to checkReturns true if the value identified by path is currently collapsed, this will return false if any of the values in path are currently not being displayed. |
public boolean | Returns: true if the node is currently collapsed, otherwise falsethe row to check, where 0 is the first row in the
display row)Returns true if the node at the specified display row is collapsed. |
public boolean | |
public boolean | Returns: true if the user is currently editing a nodeReturns true if the tree is being edited. |
public boolean | Returns: false if any of the nodes in the node's path are collapsed, true if all nodes in the path are expandedthe path)TreePath specifying the node to checkReturns true if the node identified by the path is currently expanded, |
public boolean | Returns: true if the node is currently expanded, otherwise falsethe row to check, where 0 is the first row in the
display row)Returns true if the node at the specified display row is currently expanded. |
public boolean | Returns: true if the height of each row is a fixed sizeReturns true if the height of each display row is a fixed size. |
public boolean | Returns: true if a large model is suggestedReturns true if the tree is configured for a large model. |
public boolean | Returns: true if every parent node and the node itself is editablea path)TreePath identifying a nodeReturns |
public boolean | Returns: true if the node is selecteda path)TreePath identifying a nodeReturns true if the item identified by the path is currently selected. |
public boolean | Returns: true if the root node of the tree is displayedReturns true if the root node of the tree is displayed. |
public boolean | Returns: true if the node is selectedan integer specifying a display row, where 0 is the first
row in the display row)Returns true if the node identified by row is selected. |
public boolean | Returns: true if the selection is currently emptyReturns true if the selection is currently empty. |
public boolean | |
public void | makeVisible(TreePath
the path)TreePath to make visibleEnsures that the node identified by path is currently viewable. |
protected String | Returns: a string representation of thisJTree .Overrides javax. JTree .
|
private void | readObject(ObjectInputStream
the s)ObjectInputStream from which to readHides javax. ObjectInputStream "registerValidation"
callback to update the UI for the entire tree of components
after they've all been read in.
|
protected boolean | Returns: true if a descendant was selecteda path path, boolean is includePath)true and path is selected,
it will be removed from the selection.Removes any paths in the selection that are descendants of
|
pack-priv void | removeDescendantSelectedPaths(TreeModelEvent e)
Removes any paths from the selection model that are descendants of
the nodes identified by in |
protected void | removeDescendantToggledPaths(Enumeration<TreePath>
an enumeration of the paths to remove; a value of
toRemove)null is ignoredRemoves any descendants of the |
public void | removeSelectionInterval(int
the first row to remove from the selection index0, int the last row to remove from the selection index1)Removes the specified rows (inclusive) from the selection. |
public void | removeSelectionPath(TreePath
the path)TreePath identifying a nodeRemoves the node identified by the specified path from the current selection. |
public void | removeSelectionPaths(TreePath[]
an array of paths)TreePath objects that
specifies the nodes to removeRemoves the nodes identified by the specified paths from the current selection. |
public void | removeSelectionRow(int
the row to remove row)Removes the row at the index |
public void | removeSelectionRows(int[]
an array of ints specifying display rows, where 0 is
the first row in the display rows)Removes the rows that are selected at each of the specified rows. |
public void | removeTreeExpansionListener(TreeExpansionListener
the tel)TreeExpansionListener to removeRemoves a listener for |
public void | removeTreeSelectionListener(TreeSelectionListener
the tsl)TreeSelectionListener to removeRemoves a |
public void | removeTreeWillExpandListener(TreeWillExpandListener
the tel)TreeWillExpandListener to removeRemoves a listener for |
public void | scrollPathToVisible(TreePath
the path)TreePath identifying the node to
bring into viewMakes sure all the path components in path are expanded (except for the last path component) and scrolls so that the node identified by the path is displayed. |
public void | scrollRowToVisible(int
an integer specifying the row to scroll, where 0 is the
first row in the display row)Scrolls the item identified by row until it is displayed. |
public void | |
public void | |
public void | setCellRenderer(TreeCellRenderer
the x)TreeCellRenderer that is to render each cellSets the |
public void | setDragEnabled(boolean
whether or not to enable automatic drag handling b)Turns on or off automatic drag handling. |
pack-priv Object | Returns: any saved state for this component, ornull if nonethe drop location (as calculated by
location,dropLocationForPoint ) or null
if there's no longer a valid drop locationthe state object saved earlier for this component,
or state, boolean null whether or not the method is being called because an
actual drop occurred forDrop)Overrides javax. |
public final void | |
public void | setEditable(boolean
a boolean value, true if the tree is editable flag)Determines whether the tree is editable. |
protected void | setExpandedState(TreePath
a path, boolean TreePath identifying a nodeif state)true , all parents of path and path are marked as expanded.
Otherwise, all parents of path are marked EXPANDED,
but path itself is marked collapsed.Sets the expanded state of this |
public void | setExpandsSelectedPaths(boolean
the new value for newValue)expandsSelectedPaths Configures the |
public void | setInvokesStopCellEditing(boolean
true means that newValue)stopCellEditing is invoked
when editing is interrupted, and data is saved; false means that
cancelCellEditing is invoked, and changes are lostDetermines what happens when editing is interrupted by selecting another node in the tree, a change in the tree's data, or by some other means. |
public void | setLargeModel(boolean
true to suggest a large model to the UI newValue)Specifies whether the UI should use a large model. |
public void | |
public void | |
public void | setRootVisible(boolean
true if the root node of the tree is to be displayed rootVisible)Determines whether or not the root node from
the |
public void | setRowHeight(int
the height of each cell, in pixels rowHeight)Sets the height of each cell, in pixels. |
public void | setScrollsOnExpand(boolean
false to disable scrolling on expansion;
true to enable itSets the |
public void | setSelectionInterval(int
the first index in the range to select index0, int the last index in the range to select index1)Selects the rows in the specified interval (inclusive). |
public void | setSelectionModel(TreeSelectionModel
the selectionModel)TreeSelectionModel to use,
or null to disable selectionsSets the tree's selection model. |
public void | setSelectionPath(TreePath
the path)TreePath specifying the node to selectSelects the node identified by the specified path. |
public void | setSelectionPaths(TreePath[]
an array of paths)TreePath objects that specifies
the nodes to selectSelects the nodes identified by the specified array of paths. |
public void | setSelectionRow(int
the row to select, where 0 is the first row in
the display row)Selects the node at the specified row in the display. |
public void | setSelectionRows(int[]
an array of ints specifying the rows to select,
where 0 indicates the first row in the display rows)Selects the nodes corresponding to each of the specified rows in the display. |
public void | setShowsRootHandles(boolean
true if root handles should be displayed;
otherwise, false Sets the value of the |
public void | setToggleClickCount(int
the number of mouse clicks to get a node expanded or closed clickCount)Sets the number of mouse clicks before a node will expand or close. |
public void | |
pack-priv void | |
public void | setVisibleRowCount(int
the number of rows to display newCount)Sets the number of rows that are to be displayed. |
private void | |
public void | startEditingAtPath(TreePath
the path)TreePath identifying a nodeSelects the node identified by the specified path and initiates editing. |
public boolean | Returns: true if editing was in progress and is now stopped, false if editing was not in progressEnds the current editing session. |
public void | treeDidChange()
Sent when the tree has changed enough that we need to resize the bounds, but not enough that we need to remove the expanded node set (e.g nodes were expanded or collapsed, or nodes were inserted into the tree). |
private void | unarchiveExpandedState(Object state)
Updates the expanded state of nodes in the tree based on the
previously archived state |
public void | updateUI()
Overrides javax. UIManager that the L&F has changed.
|
private void | writeObject(ObjectOutputStream
the s)ObjectOutputStream in which to writeHides javax. JComponent to an
ObjectOutputStream we temporarily uninstall its UI.
|