论坛首页 Web前端技术论坛

javaFx1.1写的一个简单tree

浏览 3330 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-10-28  
javaFx1.1没有TREE, 我继承SwingComponent类写了一个简单的tree.可添加节点,删除节点,展开等
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();
                        }}
                    
                ]
        }
    }
}
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics