📄 tree.html
字号:
</pre></blockquote><p>Here is the code that the <b>Add</b> button's event handleruses to add a new node to the tree:<blockquote><pre>treePanel.addObject("New Node " + newNodeSuffix++);...public DefaultMutableTreeNode addObject(Object child) { DefaultMutableTreeNode parentNode = null; TreePath parentPath = tree.getSelectionPath(); if (parentPath == null) { //There's no selection. Default to the root node. parentNode = rootNode; } else { parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent()); } return addObject(parentNode, child, true);}...public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); ... treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); //Make sure the user can see the lovely new node. if (shouldBeVisible) { tree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode;}</pre></blockquote>The code creates a node,inserts it into the tree model,and then, if appropriate, requests that the nodes above itbe expanded and the tree scrolled so that the new node is visible.To insert the node into the model, the code uses the <code>insertNodeInto</code> method provided by the <code>DefaultTreeModel</code> class.</blockquote><h3><a name="data">Creating a Data Model</a></h3><blockquote>If <code>DefaultTreeModel</code>doesn't suit your needs,then you'll need to write a custom data model.Your data model must implement the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeModel.html"><code>TreeModel</code></a> interface.<code>TreeModel</code> specifies methods forgetting a particular node of the tree,getting the number of children of a particular node,determining whether a node is a leaf,notifying the model of a change in the tree,and adding and removing tree model listeners.<p>Interestingly, the <code>TreeModel</code> interfaceaccepts any kind of object as a tree node.It doesn't require that nodes be representedby <code>DefaultMutableTreeNode</code> objects,or even that nodes implement the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeNode.html"><code>TreeNode</code></a> interface.Thus, if the <code>TreeNode</code> interfaceisn't suitable for your tree model,feel free to devise your own representation for tree nodes.For example,if you have a pre-existing hierarchical data structure,you don't need to duplicate itor force it into the <code>TreeNode</code> mold.You just need to implement your tree model so that it uses the information in the existing data structure.<p>The following figure shows an application calledGenealogyExamplethat displays the descendants or ancestorsof a particular person.(Thanks to tutorial readerOlivier Berlangerfor providing this example.)You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/GenealogyExample.jnlp">run GenealogyExample</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#GenealogyExample">example index</a>.<p><p><center><IMG SRC="../../figures/uiswing/components/GenealogyExample.png" WIDTH="268" HEIGHT="191" ALIGN="BOTTOM" ALT="GenealogyExample"></center></p><p>You can findthe custom tree model implementation in <a class="SourceLink" target="_blank" href="examples/GenealogyModel.java"><code>GenealogyModel.java</code></a>.Because the modelis implemented as an <code>Object</code> subclassinstead of, say, a subclass of <code>DefaultTreeModel</code>,it must implement the <code>TreeModel</code> interface directly.This requires implementing methodsfor getting information about nodes,such as which is the rootand what are the children of a particular node.In the case of <code>GenealogyModel</code>,each node is represented by an object of type <code>Person</code>,a custom class that doesn'timplement <code>TreeNode</code>.<p>A tree model must also implement methodsfor adding and removing tree model listeners,and must fire <code>TreeModelEvent</code>s to those listenerswhen the tree's structure or data changes.For example, when the user instructs GenealogyExampleto switch from showing ancestors to showing descendants,the tree model makes the changeand then fires an event to inform its listeners(such as the tree component).</blockquote><h3><a name="api">The Tree API</a></h3><blockquote>The tree API is quite extensive.The following tables list just a bit of the API,concentrating on the following categories:<ul><li><a href="#overviewapi">Tree-Related Classes and Interfaces</a><li><a href="#creatingapi">Creating and Setting Up a Tree</a><li><a href="#selectionapi">Implementing Selection</a><li><a href="#expandapi">Showing and Hiding Nodes</a></ul><p>For more information about the tree API,see the API documentation for <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html"><code>JTree</code></a> and for the various classes and interfaces in the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/package-summary.html">tree package</a>.Also refer to <a href="jcomponent.html">The JComponent Class</a>for information on the API <code>JTree</code> inheritsfrom its superclass.<p><table border=1> <caption><a name="overviewapi">Tree-Related Classes and Interfaces</a></caption><tr><th align=left>Class or Interface</th><th align=left>Purpose </th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html">JTree</a> </td> <td>The component that presents the tree to the user. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreePath.html">TreePath</a> </td> <td>Represents a path to a node. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeNode.html">TreeNode</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/MutableTreeNode.html">MutableTreeNode</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/DefaultMutableTreeNode.html">DefaultMutableTreeNode</a> </td> <td>The interfaces that the default tree model expects its tree nodes to implement, and the implementation used by the default tree model. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeModel.html">TreeModel</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/DefaultTreeModel.html">DefaultTreeModel</a> </td> <td>Respectively, the interface that a tree model must implement and the usual implementation used. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeCellRenderer.html">TreeCellRenderer</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/DefaultTreeCellRenderer.html">DefaultTreeCellRenderer</a> </td> <td>Respectively, the interface that a tree cell renderer must implement and the usual implementation used. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeCellEditor.html">TreeCellEditor</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/DefaultTreeCellEditor.html">DefaultTreeCellEditor</a> </td> <td>Respectively, the interface that a tree cell editor must implement and the usual implementation used. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeSelectionModel.html">TreeSelectionModel</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/DefaultTreeSelectionModel.html">DefaultTreeSelectionModel</a> </td> <td>Respectively, the interface that the tree's selection model must implement and the usual implementation used. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionListener.html">TreeSelectionListener</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeSelectionEvent.html">TreeSelectionEvent</a> </td> <td>The interface and event type used for detecting tree selection changes. For more information, see <a class="TutorialLink" target="_top" href="../events/treeselectionlistener.html ">Getting Started</a>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeModelListener.html">TreeModelListener</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeModelEvent.html">TreeModelEvent</a> </td> <td>The interface and event type used for detecting tree model changes. For more information, see <a class="TutorialLink" target="_top" href="../events/treemodellistener.html">How to Write a Tree Model Listener</a>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeExpansionListener.html">TreeExpansionListener</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeWillExpandListener.html">TreeWillExpandListener</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/TreeExpansionEvent.html">TreeExpansionEvent</a> </td> <td>The interfaces and event type used for detecting tree expansion and collapse. For more information, see <a class="TutorialLink" target="_top" href="../events/treeexpansionlistener.html">How to Write a Tree Expansion Listener</a> and<a class="TutorialLink" target="_top" href="../events/treewillexpandlistener.html">How to Write a Tree-Will-Expand Listener</a>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/ExpandVetoException.html">ExpandVetoException</a> </td> <td>An exception that a <code>TreeWillExpandListener</code> can throw to indicate that the impending expansion/collapse should not happen. For more information, see <a class="TutorialLink" target="_top" href="../events/treewillexpandlistener.html">How to Write a Tree-Will-Expand Listener</a>. </td> </tr></table><p><table border=1> <caption><a name="creatingapi">Creating and Setting Up a Tree</a></caption><tr><th align=left>Constructor or Method</th><th align=left>Purpose </th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#JTree(javax.swing.tree.TreeNode)">JTree(TreeNode)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#JTree(javax.swing.tree.TreeNode, boolean)">JTree(TreeNode, boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#JTree(javax.swing.tree.TreeModel)">JTree(TreeModel)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#JTree()">JTree()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#JTree(java.util.Hashtable)">JTree(Hashtable)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#JTree(java.lang.Object[])">JTree(Object[])</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#JTree(java.util.Vector)">JTree(Vector)</a> </td> <td>Create a tree. The <code>TreeNode</code> argument specifies the root node, to be managed by the default tree model. The <code>TreeModel</code> argument specifies the model that provides the data to the table. The no-argument version of this constructor is for use in builders; it creates a tree that contains some sample data. If you specify a <code>Hashtable</code>, array of objects, of <code>Vector</code> as an argument, then the argument is treated as a list of nodes under the root node (which is not displayed), and a model and tree nodes are constructed accordingly. <p> The <code>boolean</code> argument, if present, specifies how the tree should determine whether a node should be displayed as a leaf. If the argument is false (the default), any node without children is diaplayed as a leaf. If the argument is true, a node is a leaf only if its<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeNode.html#getAllowsChildren()"><code>getAllowsChildren</code></a> method returns false. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#setCellRenderer(javax.swing.tree.TreeCellRenderer)">void setCellRenderer(TreeCellRenderer)</a> </td> <td>Set the renderer that draws each node. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#setEditable(boolean)">void setEditable(boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html#setCellEditor(javax.swing.tree.TreeCellEditor)">void setCellEditor(TreeCellEditor)</a> </td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -