本文介绍了绑定HashMap< String,String>到MutableTreeNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Hashmap绑定到MutableTreeNode,以便可以在JTree中显示它.

I need to bind a Hashmap to a MutableTreeNode so that I can display that in a JTree.

我有以下代码:

static Map<String, String> form = new LinkedHashMap<String,String>();

我想这就是实现MutableTreeNode的方式.我只是不知道现在如何进行操作以使表单"的键出现在MutableTreeNode中.

I guess this is how I need to implement MutableTreeNode. I just don't know how to procees now to get the Key's of "form" to show up in the MutableTreeNode.

public class MyNode implements MutableTreeNode {

    @Override
    public Enumeration children() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean getAllowsChildren() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public TreeNode getChildAt(int childIndex) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getChildCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getIndex(TreeNode node) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public TreeNode getParent() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isLeaf() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void insert(MutableTreeNode arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void remove(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void remove(MutableTreeNode arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void removeFromParent() {
        // TODO Auto-generated method stub

    }

    @Override
    public void setParent(MutableTreeNode arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setUserObject(Object arg0) {
        // TODO Auto-generated method stub

    }

MutableTreeNode正确显示正确的Key值后,我希望能够这样做:

After the MutableTreeNode correctly shows the correct Key value, I want to be able to do:

final JTree tree = new JTree();
final DefaultMutableTreeNode CustomNode = new DefaultMutableTreeNode("Custom");
DefaultTreeModel tm = new DefaultTreeModel(CustomNode);
tree.setModel(tm);

for (String str : form.keySet())
        {
          MyNode nod = new MyNode(str);
          CustomNode.add(node);
        }
tm.reload();

推荐答案

您可能需要TreeModel接口的自定义实现.如 如何使用树:创建数据型号

You probably want a custom implementation of the TreeModel interface. As noted in How to Use Trees: Creating a Data Model,

尽管您的Map<String, String>并非本质上是分层的,但您可能会从GenealogyModel中得到一个想法. html#eg"rel =" nofollow noreferrer>此处,或 FileTreeModel 此处和.

Although your Map<String, String> is not intrinsically hierarchical, you may get an idea from the GenealogyModel, cited here, or the FileTreeModel cited here and here.

这篇关于绑定HashMap&lt; String,String&gt;到MutableTreeNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:52