facebook

JTree and Matisse

  1. MyEclipse Archived
  2.  > 
  3. Matisse/Swing UI Development
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #266848 Reply

    squiddog
    Member

    All of the JTree examples I find online involve building the tree node structure up front, and then attaching the root node to the tree using the JTree constructor:
    JTree jTree = new JTree(root);

    But when I place a JTree on a form using Matisse, it creates the JTree with no root node:
    JTree jTree = new JTree();
    and surrounds the statement with a warning saying this code should not be modified as it will be auto-updated by Matisse.

    Is there a way to add a root node to a JTree after the constructor has been called?

    Also is there a way to add more child nodes as well?

    Thanks.

    #266851 Reply

    Riyad Kalla
    Member

    You can select the components and go under Properties and add some creation code via the form designer. Like:

    
    jTree = new JTree(myRootNode);
    

    and that code will replace the default generated constructor code.

    #266857 Reply

    squiddog
    Member

    Thank you – that worked.

    About the second part. Is there a way to add nodes post-constructor? I have a huge tree to build from a database. The way I did it in Windows was through virtual loading the tree, but this is all very new to me.

    Thanks!

    #266858 Reply

    Riyad Kalla
    Member

    Yea, you want to create your root node separately, make sure it’s an instance of DefaultTreeNode (remembering the class off the top of my head, might be different). And what you do is make that the root node during the JTree constructions.

    Then you keep the reference around and when loading from your database, use your DefaultTreeNode reference to populate the tree, the actual JTree will reacte accordingly and update itself as more nodes are added.

    NOTE: The trick is that TreeNode is a read-only interface, but DefaultTreeNode is a concrete implementation that adds all the mutation methods (add/remove/etc.) so you need to keep around and use the actual DefaultTreeNode reference, or just go a JTree.getRootNode and cast it back to DefaultTreeNode.

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: JTree and Matisse

You must be logged in to post in the forum log in