开发者

Displaying Jtree on selecting item from JComboBox?

开发者 https://www.devze.com 2023-02-11 21:06 出处:网络
I need to construct tree on action listener of JComboBox public class ExpressionTree extends JFrame implements ActionListener {

I need to construct tree on action listener of JComboBox

public class ExpressionTree extends JFrame implements ActionListener {

    private JComboBox group;    
    private JLabel groupLabel;
    Container content;
    JTree tree;
    public static void main(String[] args) {
        new ExpressionTree();
    }


    public ExpressionTree() {

        super("Creating a Simple JTree");
        WindowUtilities.setNativeLookAndFeel();
        addWindowListener(new ExitListener());
        content = getContentPane();

        group = new JComboBox();
        groupLabel = new JLabel("GROUP");

        ExpressionBuilder expressionBuilder = new ExpressionBuilder(); 
        Set<Integer> set = expressionBuilder.getGroup().keySet();
        Iterator<Integer> iterator = set.iterator();
        group.addItem("select");
        while(iterator.hasNext()){
            group.addItem(expressionBuilder.getGroup().get(iterator.next()));       
        }


        content.add(groupLabel);
        content.add(group);
        group.addActionListener(this);
        content.setLayout(new FlowLayout());

        tree = new JTree();
        content.add(n开发者_StackOverflowew JScrollPane(tree), BorderLayout.CENTER);
        setSize(300, 475);
        setVisible(true);

    }
    public String groupString = null;
    public int groupId;

    public void actionPerformed(ActionEvent e){

        groupString = group.getSelectedItem().toString();
        ExpressionBuilder builder=new ExpressionBuilder();
        builder.getGroupId(groupString);
        _____//what to write here to see the tree
    }
}

public class NodeTree  extends DefaultMutableTreeNode{

    private JTree tree;
    public NodeTree(){
            ExpressionTree expressionTree= new ExpressionTree();
            String groupstring = expressionTree.groupString;
            int groupId = expressionTree.groupId;
            DefaultMutableTreeNode top = new DefaultMutableTreeNode(groupstring);
            tree = new JTree(top);
            tree.setEditable(true);
            JScrollPane treeView = new JScrollPane(tree);
            createNode(top,groupId);

    }

    public void createNode(DefaultMutableTreeNode top,int group){
        DefaultMutableTreeNode expression[] = null;
        ExpressionBuilder builder=new ExpressionBuilder();
        Object object = (Object)builder.getExpression(group,0);
        top.add(expression[1]);
        expression[1] = new DefaultMutableTreeNode(object);     
        expression[1].add(new     DefaultMutableTreeNode((Object)builder.getExpression(group,1)));

    }
}

i am using above file to create a tree.


tree.setModel(new DefaultTreeModel(theTreeRootNode));
0

精彩评论

暂无评论...
验证码 换一张
取 消