Custom Models and Dynamic Trees

Custom Models and Dynamic Trees

      A JTreeuses a TreeModelto get its data. As with JList, you can replace the model altogether, specifying how to extract data from the custom model. See the tutorial section on JListfor an example of this general approach.


In the case of JTree, however, the default TreeModeluses a TreeNodeto store data associated with the tree, and it is more common to leave the TreeModelunchanged and instead make a custom TreeNode. The easiest approach for that is to start with DefaultMutableTreeNode. One of the common cases where this is useful is when you don't want to explicitly lay out each node in the tree, but instead you have some sort of algorithm that describes the children of a given node, and you want to build the tree dynamically, only actually generating children for places that the user expands. For instance, in the following example the tree is potentially infinite, with each node describing a section in an outline. The root will be "1", the first-level children will be 1.1, 1.2, 1.3, etc., the second-level children will be 1.1.1, 1.1.2, etc., and so forth. The actual number of children of each node will be determined by a command-line argument to the program.


The key to building a JTreedynamically is to observe that getChildCount(a method in DefaultMutableTreeNode) will be called before any of the children will actually be retrieved. So you keep a flag indicating whether children have ever been built. So you wait until getChildCountis called, then, if the flag is false, build the children and add them. To keep the tree from trying to count the children (and thus build the nodes) in order to determine which nodes are leaf nodes, override isLeafto always return false.


1.1 Dynamic Tree: Example Code(Download source code)



import java.awt.*;


import javax.swing.*;


public class DynamicTree extends JFrame {


        public static void main(String[] args) {


        int n = 5; // Number of children to give each node


        if (args.length >0)


        try {


        n = Integer.parseInt(args[0]);


        } catch(NumberFormatException nfe) {


        System.out.println("Can't parse number; using default of " + n);


        }


        new DynamicTree(n);


        }


        public DynamicTree(int n) {


        super("Creating a Dynamic JTree");


        WindowUtilities.setNativeLookAndFeel();


        addWindowListener(new ExitListener());


        Container content = getContentPane();


        JTree tree = new JTree(new OutlineNode(1, n));


        content.add(new JScrollPane(tree), BorderLayout.CENTER);


        setSize(300, 475);


        setVisible(true);


        }


}


Note: also requiresWindowUtilities.javaand ExitListener.java, shown earlier.


1.2 OutlineNode.java(Download source code)



import java.awt.*;


import javax.swing.*;


import javax.swing.tree.*;


public class OutlineNode extends DefaultMutableTreeNode
{


        
private boolean areChildrenDefined = false;


        private int outlineNum;


        private int numChildren;


        public OutlineNode(int outlineNum, int numChildren) {


        this.outlineNum = outlineNum;


        this.numChildren = numChildren;


        }


        public boolean isLeaf() {



        
return(false);



        }



public int getChildCount() {


        
if (!areChildrenDefined)



        
defineChildNodes();



        
return(super.getChildCount());



        }



private void defineChildNodes() {


// You must set the flag before defining children if you


        // use "add" for the new children. Otherwise you get an infinite


        // recursive loop, since add results in a call to getChildCount.


        // However, you could use "insert" in such a case.


        areChildrenDefined = true;



        
for(int i=0; i



        
add(new OutlineNode(i+1, numChildren));



        }



public String toString() {


        TreeNode parent = getParent();


        if (parent == null)


        return(String.valueOf(outlineNum));


        else


        return(parent.toString() + "." + outlineNum);


        }


}


 1.3. Dynamic Tree: Initial Result

 


 
1.4. Dynamic Tree: Result After Expanding A Few Nodes

 



标签词:

JTree new DefaultMutableTreeNode tree You children public nodes import Child

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值