Search code examples
javaswinglistenerjtree

When editing a node value in jtree, i need a listener to keep track, but how should i do it?


My question has something to do with this link too. http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#data

I am modifying a parser program that use Jtree. The way the Jtree is created in my program is very similar to example in the link i posted above. Let's call it CREATE-JTREE-METHOD-1.

DefaultMutableTreeNode top =
    new DefaultMutableTreeNode("The Java Series");
createNodes(top);
tree = new JTree(top);
...
JScrollPane treeView = new JScrollPane(tree);
...

private void createNodes(DefaultMutableTreeNode top) {
    DefaultMutableTreeNode category = null;
    DefaultMutableTreeNode book = null;

    category = new DefaultMutableTreeNode("Books for Java Programmers");
    top.add(category);

    book = new DefaultMutableTreeNode(new BookInfo
       ("The Java Tutorial: A Short Course on the Basics",
       "tutorial.html"));
    category.add(book);
    ...
}

Basically a Jtree with "top" node is created first, then nodes with text value are "added" to 'top' node as they being parsed out.

My job is to try to manually change the value of a single node, and do something in this event listener.

In the "Dynamically Changing a Tree" section of the same link i posted. It gives following example to implement node change listener,

rootNode = new DefaultMutableTreeNode("Root Node");
treeModel = new DefaultTreeModel(rootNode);
treeModel.addTreeModelListener(new MyTreeModelListener());

tree = new JTree(treeModel);

However, this Jtree is constructed using DefaultTreeModel (Let's call it CREATE-JTREE-METHOD-2). It does not give sample code on how data is added to the model though.

I don't know if there's a way to get the data model out of a Jtree created with method 1, then use the addTreeModelListener() method as shown? In other word, does method 1 indeed use DefaultTreeModel too?

I tried to search for something like getDefaultTreeModel(), but with no success.

If the above tree model thing does not work, any other way i can do a track to node value being changed on Jtree?

Thanks in advance,


Solution

  • you can create the tree using a default tree model...

    ...and later retrive that model by simply getting it from the tree (yes, you were right, but it is working ^^

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);
    
    DefaultTreeModel model2 = (DefaultTreeModel)tree.getModel; //<-- you'll have to cast it!
    

    but you want to get the root node directrly from the jtree... it's not accessable directly... reading source code reveals:

    /**
     * Creates a new <code>JTree</code> object.
     * 
     * @param root the root node
     */
    public JTree(TreeNode root)
    {
        this(root, false);
    }
    
    /**
     * Creates a new <code>JTree</code> object.
     * 
     * @param root the root node
     * @param asksAllowChildren if false, all nodes without children are leaf
     *        nodes. If true, only nodes that do not allow children are leaf
     *        nodes.
     */
    public JTree(TreeNode root, boolean asksAllowChildren)
    {
        this(new DefaultTreeModel(root, asksAllowChildren));
    }
    

    (from http://developer.classpath.org/doc/javax/swing/JTree-source.html)

    when you create a jtree with a root node the tableModle is always created... (like when you write new JTree(new TreeNode() );

    so - just skip the part where you create a JTree with a root node and do as proposed above - create it with a treeModel.. there you can add (as proposed by yourself) the treeModelListener