从JComboBox中选择项目时显示Jtree?

时间:2011-02-17 11:50:38

标签: java swing jtree

我需要在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(new 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)));

    }
}

我使用上面的文件来创建一个树。

1 个答案:

答案 0 :(得分:1)

tree.setModel(new DefaultTreeModel(theTreeRootNode));