1

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

I have the following code:

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

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

    }

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();
4
  • unrelated: Please learn java naming conventions and stick to them. Commented Apr 19, 2013 at 14:07
  • what is difficult to understand in naming conventions? Commented Apr 19, 2013 at 14:26
  • what is wrong in my question? Commented Apr 19, 2013 at 14:41
  • shaking head and gritting teeth: see my first comment (hint: it's an advice you should follow ... implication being that you don't ;) Commented Apr 19, 2013 at 14:44

1 Answer 1

3

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

The TreeModel interface accepts any kind of object as a tree node. It does not require that nodes be represented by DefaultMutableTreeNode objects, or even that nodes implement the TreeNode interface.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.