如何将列表转换为到 DefaultMutableTreeNode?

发布于 2024-10-18 00:53:16 字数 2626 浏览 5 评论 0原文

如何将列表转换为DefaultMutableTreeNode?

我正在从列表中的数据库中获取数据。现在我想将该列表转换为 JTree。我该怎么做?

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.getSelectedItem().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)));

    }

}

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.getSelectedItem().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)));

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2024-10-25 00:53:16

假设您从数据库中正确获取节点名称并且节点是扁平的(无层次结构),您可以使用此代码创建一棵树

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;
  }

注意:我创建了虚拟节点作为根并使用了 setRootVisible(false) 。如果您有有效的根节点,则可以使用它并删除隐藏根的行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文