saidreddy
asked on
Adding a node to a JTree at user command
Hello,
I have created a JTree without any node. The tree is blank. The user has say create node then comes a dialog where the user has to enter the number and when he says ok then this number should be created as a node there. And since the tree is empty i am getting is a create icon which i want it to be removed.
So what i need is
1) Node should be created at usercommand
2) When the empty tree is created then the icon created should be removed the tree should be blank when i start my gui.
My code is as follows.
DefaultMutableTreeNode node = new DefaultMutableTreeNode("") ;
DefaultTreeModel tModel = new DefaultTreeModel(node);
JTree tree = new Jtree(tModel);
So this code creates an empty JTree with a create icon as the root element which should be removed
and when the user presses the ok button of the dialog the value in the dialog box should be used to create a tree.
Thanks in advance
regards,
said
I have created a JTree without any node. The tree is blank. The user has say create node then comes a dialog where the user has to enter the number and when he says ok then this number should be created as a node there. And since the tree is empty i am getting is a create icon which i want it to be removed.
So what i need is
1) Node should be created at usercommand
2) When the empty tree is created then the icon created should be removed the tree should be blank when i start my gui.
My code is as follows.
DefaultMutableTreeNode node = new DefaultMutableTreeNode("")
DefaultTreeModel tModel = new DefaultTreeModel(node);
JTree tree = new Jtree(tModel);
So this code creates an empty JTree with a create icon as the root element which should be removed
and when the user presses the ok button of the dialog the value in the dialog box should be used to create a tree.
Thanks in advance
regards,
said
Have a look here: http://javaalmanac.com/egs/javax.swing.tree/AddNode.html
Look at the example at http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
The paragraph "Dynamically Changing a Tree"
And here for more info: http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JTree.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
HI
I have used the command tModel.setRoot(newNode) to set the root element.
Now i need to check whether the root element is empty or not and which i have done i am checking this with a if condition whether the root is empty or not if it is empty then the number i am entering in the dialog box is set as root element. This works fine with me. BUt when i say create node again after the root node has been created and enter a new number it is creating a node as a child to the root element which i do not want to it should create as a normal parent element in the same line as the root element.
For example i say create node 4587
1234(root element)
4587
but not as
1234
----4587
regards
said
I have used the command tModel.setRoot(newNode) to set the root element.
Now i need to check whether the root element is empty or not and which i have done i am checking this with a if condition whether the root is empty or not if it is empty then the number i am entering in the dialog box is set as root element. This works fine with me. BUt when i say create node again after the root node has been created and enter a new number it is creating a node as a child to the root element which i do not want to it should create as a normal parent element in the same line as the root element.
For example i say create node 4587
1234(root element)
4587
but not as
1234
----4587
regards
said
ASKER
hi all,
I did find a solution finally and thanks all for all your help.
thanks
reg,
said
I did find a solution finally and thanks all for all your help.
thanks
reg,
said