Oracle官方文档JTree技术摘编
2013-09-11 21:30
357 查看
How to use a JTreee
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.
JTree, by default, renders each node using the value returned from toString, so it is important that
convertValueToText of JTree to map the object from the model into a string that gets displayed.
To summarize, you can create a tree by invoking the
do not have to do anything to make the tree nodes expand and collapse in response to user clicks. However, you do have to add some code to make the tree respond when the user selects a node — by clicking the node, for example.
The preceding code performs these tasks:
Gets the default
Registers an event handler on the tree. The event handler is an object that implements the
interface.
In the event handler, determines which node is selected by invoking the tree's
Uses the
For more details about handling tree selection events, see
How to Write a Tree Selection Listener.
Here is the code that initializes the tree:
By explicitly creating the tree's model, the code guarantees that the tree's model is an instance of
That way, we know all the methods that the tree model supports. For example, we know that we can invoke the model's
To make the text in the tree's nodes editable, we invoke
nodes have changed. Note that although
know about the updates.
To be notified of node changes, we can implement a
http://stackoverflow.com/questions/11554583/jtree-node-rename-preserve-user-object
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.
private JTree tree; ... public TreeDemo() { ... DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series"); createNodes(top); tree = new JTree(top); ... JScrollPane treeView = new JScrollPane(tree); ... }
JTree, by default, renders each node using the value returned from toString, so it is important that
toStringreturns something meaningful. Sometimes, it is not feasible to override
toString; in such a scenario you can override the
convertValueToText of JTree to map the object from the model into a string that gets displayed.
To summarize, you can create a tree by invoking the
JTreeconstructor, specifying the class that implements TreeNode as an argument. You should probably put the tree inside a scroll pane, so that the tree would not take up too much space. You
do not have to do anything to make the tree nodes expand and collapse in response to user clicks. However, you do have to add some code to make the tree respond when the user selects a node — by clicking the node, for example.
Responding to Node selection
Responding to tree node selections is simple. You implement a tree selection listener and register it on the tree. The following code shows the selection-related code from theTreeDemoprogram:
//Where the tree is initialized: tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION); //Listen for when the selection changes. tree.addTreeSelectionListener(this); ... public void valueChanged(TreeSelectionEvent e) { //Returns the last path element of the selection. //This method is useful only when the selection model allows a single selection. DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node == null) //Nothing is selected. return; Object nodeInfo = node.getUserObject(); if (node.isLeaf()) { BookInfo book = (BookInfo)nodeInfo; displayURL(book.bookURL); } else { displayURL(helpURL); } }
The preceding code performs these tasks:
Gets the default
TreeSelectionModelfor the tree, and then sets it up so that at most one tree node at a time can be selected.
Registers an event handler on the tree. The event handler is an object that implements the
TreeSelectionListener
interface.
In the event handler, determines which node is selected by invoking the tree's
getLastSelectedPathComponentmethod.
Uses the
getUserObjectmethod to get the data associated with the node.
For more details about handling tree selection events, see
How to Write a Tree Selection Listener.
Dynamically changing a Tree
rootNode = new DefaultMutableTreeNode("Root Node"); treeModel = new DefaultTreeModel(rootNode); treeModel.addTreeModelListener(new MyTreeModelListener()); tree = new JTree(treeModel); tree.setEditable(true); tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION); tree.setShowsRootHandles(true);
By explicitly creating the tree's model, the code guarantees that the tree's model is an instance of
DefaultTreeModel.
That way, we know all the methods that the tree model supports. For example, we know that we can invoke the model's
insertNodeIntomethod, even though that method is not required by the
TreeModelinterface.
To make the text in the tree's nodes editable, we invoke
setEditable(true)on the tree. When the user has finished editing a node, the model generates a tree model event that tells any listeners — including the
JTree— that tree
nodes have changed. Note that although
DefaultMutableTreeNodehas methods for changing a node's content, changes should go through the
DefaultTreeModelcover methods. Otherwise, the tree model events would not be generated, and listeners such as the tree would not
know about the updates.
To be notified of node changes, we can implement a
TreeModelListener. Here is an example of a tree model listener that detects when the user has typed in a new name for a tree node:
class MyTreeModelListener implements TreeModelListener { public void treeNodesChanged(TreeModelEvent e) { DefaultMutableTreeNode node; node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent()); /* * If the event lists children, then the changed * node is the child of the node we have already * gotten. Otherwise, the changed node and the * specified node are the same. */ try { int index = e.getChildIndices()[0]; node = (DefaultMutableTreeNode) (node.getChildAt(index)); } catch (NullPointerException exc) {} System.out.println("The user has finished editing the node."); System.out.println("New value: " + node.getUserObject()); } public void treeNodesInserted(TreeModelEvent e) { } public void treeNodesRemoved(TreeModelEvent e) { } public void treeStructureChanged(TreeModelEvent e) { } }
http://stackoverflow.com/questions/11554583/jtree-node-rename-preserve-user-object
相关文章推荐
- Oracle官方文档学习路线图
- Oracle官方文档中关于DUPLICATE的文章
- oracle官方文档
- Oracle官方文档结构说明
- [官方文档] oracle官方文档总汇(9i,10g,11gR1, 11gR2)
- Android官方技术文档翻译——Gradle 插件用户指南(4)
- 如何查看 oracle 官方文档
- oracle 12c 官方文档 及软件下载
- Android官方技术文档翻译——Gradle 插件用户指南(4)
- oracle官方文档总汇(9i,10g,11gR1, 11gR2)
- java中的反射 1—— 简介@译自Oracle官方文档
- Oracle 官方文档阅读顺序
- (转)oracle官方文档介绍
- Android官方技术文档翻译——ApplicationId 与 PackageName
- Spring 4 官方文档学习(七)核心技术之Spring AOP APIs
- Android官方技术文档翻译——Ant 任务
- [技术翻译]Guava官方文档Ordering
- 根据红帽RHEL7官方文档对centos7进行技术剖析(一)
- 如何在oracle官方网站下载JavaSE官方文档
- oracle官方文档总汇(9i,10g,11gR1,11gR2)