开发者

How to convert list<String> to DefaultMutableTreeNode?

How to convert list to DefaultMutableTreeNode?

I am fetching data from database in list. Now i want to convert that list to JTree. How can i do it?

public class ExpressionTree extends JFrame implements ActionListener {

private JComboBox group;    
private JLabel groupLabel;
Container content;
JTree tree;
DefaultMutableTreeNode root, child;

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());
    root= new DefaultMutableTreeNode("root");
    tree = new JTree(root);
    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.getS开发者_JAVA技巧electedItem().toString();
    ExpressionBuilder builder=new ExpressionBuilder();
    builder.getGroupId(groupString);
    //tree.setModel(new NodeTree());.........//what to write over here
    //root.add(new NodeTree());
}
}


public class NodeTree  extends DefaultMutableTreeNode{

    private JTree tree;
    DefaultMutableTreeNode top; 
    public NodeTree(){
            ExpressionTree expressionTree= new ExpressionTree();
            String groupstring = expressionTree.groupString;
            int groupId = expressionTree.groupId;
            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);
        expression[1] = new DefaultMutableTreeNode(object);  
        top.add(expression[1]);
        expression[1].add(new DefaultMutableTreeNode((Object)builder.getExpression(group,1)));

    }

}


Assuming that you are fetching the node names properly from database and the nodes are flat (no hierarchy), you can create a tree with this code

private JTree createTree(List<String> nodeNames) {

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(new Object());
    TreeModel model = new DefaultTreeModel(root);
    for (int i = 0; i < nodeNames.size(); i++) {
      root.add(new DefaultMutableTreeNode(nodeNames.get(i)));
    }
    JTree tree = new JTree(model);
    tree.setRootVisible(false);
    return tree;
  }

Note: I created dummy node as root and used the setRootVisible(false). If you have a valid root node you can use it and remove the line that is hiding the root.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜