JTree用法及JTree使用经验总结

importjava.awt.Dimension;
importjava.awt.Color;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTree;
importjavax.swing.BoxLayout;
importjavax.swing.tree.TreePath;
importjavax.swing.tree.DefaultMutableTreeNode;
importjavax.swing.tree.DefaultTreeModel;
/*
JTree的构造函数:
JTree()
JTree(Hashtablevalue)
JTree(Object[]value)//只有这个构造函数可以创建多个根结点
JTree(TreeModelnewModel)
JTree(TreeNoderoot)
JTree(TreeNoderoot,booleanasksAllowsChildren)
JTree(Vectorvalue)

*/
publicclassJTreeDemo
{
publicstaticvoidmain(String[]args)
{


//构造函数:JTree()
JTreeexample1=newJTree();




//构造函数:JTree(Object[]value)
Object[]letters={"a","b","c","d","e"};
JTreeexample2
=newJTree(letters);




//构造函数:JTree(TreeNoderoot)(TreeNode空)
//用空结点创建树
DefaultMutableTreeNodenode1=newDefaultMutableTreeNode();//定义树结点
JTreeexample3=newJTree(node1);//用此树结点做参数调用JTree的构造函数创建含有一个根结点的树




//构造函数:JTree(TreeNoderoot)(同上,只是TreeNode非空)
//用一个根结点创建树
DefaultMutableTreeNodenode2=newDefaultMutableTreeNode("Color");
JTreeexample4
=newJTree(node2);//结点不可以颜色,默认为白面黑字
example4.setBackground(Color.lightGray);




//构造函数:JTree(TreeNoderoot,booleanasksAllowsChildren)(同上,只是TreeNode又有不同)
//使用DefaultMutableTreeNode类先用一个根结点创建树,设置为可添加孩子结点,再添加孩子结点
DefaultMutableTreeNodecolor=newDefaultMutableTreeNode("Color",true);
DefaultMutableTreeNodegray
=newDefaultMutableTreeNode("Gray");
color.add(gray);
color.add(
newDefaultMutableTreeNode("Red"));
gray.add(
newDefaultMutableTreeNode("Lightgray"));
gray.add(
newDefaultMutableTreeNode("Darkgray"));
color.add(
newDefaultMutableTreeNode("Green"));
JTreeexample5
=newJTree(color);




//构造函数:JTree(TreeNoderoot)(同上,只是TreeNode非空)
//通过逐个添加结点创建树
DefaultMutableTreeNodebiology=newDefaultMutableTreeNode("Biology");
DefaultMutableTreeNodeanimal
=newDefaultMutableTreeNode("Animal");
DefaultMutableTreeNodemammal
=newDefaultMutableTreeNode("Mammal");
DefaultMutableTreeNodehorse
=newDefaultMutableTreeNode("Horse");
mammal.add(horse);
animal.add(mammal);
biology.add(animal);
JTreeexample6
=newJTree(biology);
horse.isLeaf();
horse.isRoot();




//构造函数:JTree(TreeModelnewModel)
//用DefaultMutableTreeNodel类定义一个结点再用这个结点做参数定义一个用DefaultTreeMode
//创建一个树的模型,再用JTree的构造函数创建一个树

DefaultMutableTreeNoderoot
=newDefaultMutableTreeNode("Root1");
DefaultMutableTreeNodechild1
=newDefaultMutableTreeNode("Child1");
DefaultMutableTreeNodechild11
=newDefaultMutableTreeNode("Child11");
DefaultMutableTreeNodechild111
=newDefaultMutableTreeNode("Child111");
root.add(child1);child1.add(child11);child11.add(child111);



DefaultTreeModelmodel
=newDefaultTreeModel(root);

JTreeexample7
=newJTree(model);



JPanelpanel
=newJPanel();
panel.setLayout(
newBoxLayout(panel,BoxLayout.X_AXIS));
panel.setPreferredSize(
newDimension(700,400));
panel.add(
newJScrollPane(example1));//JTree必须放在JScrollPane上
panel.add(newJScrollPane(example2));
panel.add(
newJScrollPane(example3));
panel.add(
newJScrollPane(example4));
panel.add(
newJScrollPane(example5));
panel.add(
newJScrollPane(example6));
panel.add(
newJScrollPane(example7));




JFrameframe
=newJFrame("JTreeDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.show();
}
}

在实际开发过程中会经常使用JTree组件,平时会遇到这样或那样的问题,在此将偶得一点经验写下来,与大家共享,希望对大家有所帮助。

privateJTreejtNetDevice;//数组件申明
privateJScrollPanejspTree;//滚动面板申明


1、初始化
DefaultMutableTreeNoderootNode=newDefaultMutableTreeNode("root");
jtNetDevice=newJTree(rootNode);
jtNetDevice.setAutoscrolls(true);
getTreeSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);//设置单选模式
jspTree=newJScrollPane();
jspTree.getViewport().add(jtNetDevice,null);

2、三个经常使用的取值函数
privateDefaultTreeModelgetTreeModel(){
return(DefaultTreeModel)jtNetDevice.getModel();
}

privateDefaultMutableTreeNodegetRootNode(){
return(DefaultMutableTreeNode)getTreeModel().getRoot();
}

privateTreeSelectionModelgetTreeSelectionModel(){
returnjtNetDevice.getSelectionModel();
}

3、根据node得到path:
TreePathvisiblePath=newTreePath(getTreeModel().getPathToRoot(node));

4、根据Path展开到该节点
jtNetDevice.makeVisible(visiblePath);

5、根据path设定该节点选定
jtNetDevice.setSelectionPath(visiblePath);

6、选中节点的方法
首先,根据节点得到树路径,其中chosen为需要选中的节点
TreePathvisiblePath=newTreePath(((DefaultTreeModel)jtNetDevice.getModel()).
getPathToRoot(chosen));
然后根据Path选中该节点
jtNetDevice.setSelectionPath(visiblePath);

7、滚动到可见位置
jtNetDevice.scrollPathToVisible(visiblePath);

8、给JTree添加右键弹出菜单
voidjtNetDevice_mouseReleased(MouseEvente){
if(e.isPopupTrigger()){
jPopupMenu1.show(e.getComponent(),e.getX(),e.getY());//弹出右键菜单
}
}

9、关于JTree的展开
//Ifexpandistrue,expandsallnodesinthetree.
//Otherwise,collapsesallnodesinthetree.
publicvoidexpandAll(JTreetree,booleanexpand){
TreeNoderoot=(TreeNode)tree.getModel().getRoot();

//Traversetreefromroot
expandAll(tree,newTreePath(root),expand);
}
privatevoidexpandAll(JTreetree,TreePathparent,booleanexpand){
//Traversechildren
TreeNodenode=(TreeNode)parent.getLastPathComponent();
if(node.getChildCount()>=0){
for(Enumeratione=node.children();e.hasMoreElements();){
TreeNoden=(TreeNode)e.nextElement();
TreePathpath=parent.pathByAddingChild(n);
expandAll(tree,path,expand);
}
}

//Expansionorcollapsemustbedonebottom-up
if(expand){
tree.expandPath(parent);
}else{
tree.collapsePath(parent);
}
}

10、如何遍历JTree
//创建树
JTreetree=newJTree();

//添加树节点......

//遍历所有节点
visitAllNodes(tree);

//仅遍历展开的节点
visitAllExpandedNodes(tree);

//Traverseallnodesintree
publicvoidvisitAllNodes(JTreetree){
TreeNoderoot=(TreeNode)tree.getModel().getRoot();
visitAllNodes(root);
}
publicvoidvisitAllNodes(TreeNodenode){
//nodeisvisitedexactlyonce
process(node);

if(node.getChildCount()>=0){
for(Enumeratione=node.children();e.hasMoreElements();){
TreeNoden=(TreeNode)e.nextElement();
visitAllNodes(n);
}
}
}

//Traverseallexpandednodesintree
publicvoidvisitAllExpandedNodes(JTreetree){
TreeNoderoot=(TreeNode)tree.getModel().getRoot();
visitAllExpandedNodes(tree,newTreePath(root));
}
publicvoidvisitAllExpandedNodes(JTreetree,TreePathparent){
//Returnifnodeisnotexpanded
if(!tree.isVisible(parent)){
return;
}

//nodeisvisibleandisvisitedexactlyonce
TreeNodenode=(TreeNode)parent.getLastPathComponent();
process(node);

//Visitallchildren
if(node.getChildCount()>=0){
for(Enumeratione=node.children();e.hasMoreElements();){
TreeNoden=(TreeNode)e.nextElement();
TreePathpath=parent.pathByAddingChild(n);
visitAllExpandedNodes(tree,path);
}
}
}


你可能感兴趣的:(构造函数,JTree使用经验,JTree用法)