0
votes

I read many topics about this but still have some problems. I'm using java desktop project from NB. I have created tree from palette and now after every click of button I want to create new tree and refresh it. So I have event action performed where I want make new jTree add some DefaultMutableTreeNode's and show this in window. Any ideas?

Maybe in other words how should I create Jtree to modify it content? I make something like this now: in initComponents jTree1 = new JTree(nodeF); where nodeF is my field (DefaultMutableTreeNode) initialised before initComponents and then I want to modify this node element adding and removing another nodes.

I'm able to refresh tree ((DefaultTreeModel) jTree1.getModel()).reload(); but I'm unable to create new instance of nodeF

I fell like I'm making some stupid mistake.. dont know how to create gui right..

2

2 Answers

5
votes

First idea, get away from Netbeans. Using a GUI editor prevents you from learning important parts of Swing, and generates code that is awful to debug or customize.

Second idea, it sounds like you could get away with just refreshing the existing tree and removing the current content by setting the root. That way you don't have to create a new tree each time.

2
votes

Is the tree structure actually changing? Or are you just expanding/collapsing nodes in the tree? My guess is the latter.

You should probably change your data model objects to implement TreeNode. The JTree will query your TreeNode objects as needed to determine which ones have children, what the children are, etc.

To expand/contract nodes in the tree without using the built-in tree controls, use the methods in JTree, e.g. expandPath or expandRow.