浏览 3330 次
锁定老帖子 主题:javaFx1.1写的一个简单tree
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-10-28
IDE:netBeans6.5.1 javaFx1.1 package com.tongye.client.control; import javafx.ext.swing.SwingComponent; import javax.swing.JComponent; import javax.swing.JTree; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.ext.swing.SwingScrollPane ; import javafx.scene.paint.Color ; import javax.swing.tree.TreeNode; import javax.swing.tree.*; import javax.swing.*; import javafx.ext.swing.SwingButton; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.*; import java.util.*; import java.lang.*; /* * 简单的树, 一个根节点,多个同级子节点 * */ public class FxTree extends SwingComponent { public var fleldNames:java.util.List; //字段名 public var rootNodeName:String= ""; //根节点的名称 public var showSelectName:function(name:String); var jTree:JTree; var root:DefaultMutableTreeNode; var model:DefaultTreeModel ; init{ if(rootNodeName!=null){ root=new DefaultMutableTreeNode(rootNodeName); model.setRoot(root); //修改树的根节点的名称为 表名 if(fleldNames!=null){ var i=0; while(i<fleldNames.size()){ addNodeAtRoot(fleldNames.get(i)); //为树添加一级子节点 i++; } } } } public override function createJComponent():JComponent { jTree = new JTree (new DefaultMutableTreeNode("请先选择表")); model = jTree.getModel() as DefaultTreeModel; jTree.setToggleClickCount(1);//设置节点展开或关闭之前鼠标的单击数。 jTree.addTreeSelectionListener(FxTreeListener{fxTree:this}) ; return jTree; } public function action(treePath:TreePath){ var s=treePath.getLastPathComponent(); var bf:StringBuffer = new StringBuffer(); bf.append(s); showSelectName(bf.toString()); } //删除根节点下全部子节点 public function delAllNode(){ try{ while(true){ var n:DefaultMutableTreeNode= root.getFirstChild()as DefaultMutableTreeNode; model.removeNodeFromParent(n); } }catch(e){ return ;//如果此节点没有子节点,则抛出 NoSuchElementException。 } } //在根节点上添加子节点 public function addNodeAtRoot(name){ var newNode :DefaultMutableTreeNode = new DefaultMutableTreeNode(name); model.insertNodeInto(newNode, root, root.getChildCount()); //展开 //jTree.expandPath(new TreePath( model.getPathToRoot(root))); } //全部展开 public function expandAll() { var root:TreeNode = model.getRoot() as TreeNode; expandAll(jTree, new TreePath(root), true); } public function expandAll( tree:JTree, parent:TreePath, expand:Boolean) { var node:TreeNode = parent.getLastPathComponent() as TreeNode; if (node.getChildCount() >= 0) { var e:java.util.Enumeration =node.children(); while ( e.hasMoreElements() ) { var n:TreeNode = e.nextElement() as TreeNode; var path:TreePath = parent.pathByAddingChild(n); //println("展开..."); //expandAll(tree, path, expand); } } if (expand) { tree.expandPath(parent); } else { tree.collapsePath(parent); } } } public function run(){ var tree= FxTree{rootNodeName:"信息表"} Stage { title: "树示例" width: 650 height: 450 scene: Scene { content:[ SwingScrollPane{ view: tree, height:300, width:400, scrollable:true, cache:true, foreground:Color.ORANGE } SwingButton{text:"删除全部节点",translateX:400,translateY:40,action:function(){ tree.delAllNode(); }} SwingButton{text:"添加一个节点",translateX:400,translateY:70,action:function(){ tree.addNodeAtRoot("新节点"); }} SwingButton{text:"全部展开",translateX:400,translateY:160,action:function(){ tree.expandAll(); }} ] } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |