- This topic has 3 replies, 2 voices, and was last updated 17 years, 11 months ago by Riyad Kalla.
-
AuthorPosts
-
squiddogMemberAll 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.
Riyad KallaMemberYou 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.
squiddogMemberThank 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!
Riyad KallaMemberYea, 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.
-
AuthorPosts