您的位置:首页 > 数据库 > Oracle

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.

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
toString
returns 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
JTree
constructor, 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 the
TreeDemo
program:

//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
TreeSelectionModel
for 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
getLastSelectedPathComponent
method.
Uses the
getUserObject
method 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


Here is the code that initializes the 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
insertNodeInto
method, even though that method is not required by the
TreeModel
interface.

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
DefaultMutableTreeNode
has methods for changing a node's content, changes should go through the
DefaultTreeModel
cover 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: