[Dynamic Tree] Add new node in a leaf

asked 2014-08-07 08:47:03 +0800

ZKL gravatar image ZKL
3 1

I want to create a dynamic tree so that when I press a button from node/leaf item(let say my node/and leaf has a label: "NodeName", and a button: "Add Child") it adds a new child to the selected node/leaf. Now I can add children to a node but can't add children to a leaf. I need also to add children to a leaf.

I have an idea to convert a leaf into node and after that to add children to the leaf-node. Any idea how implement this or any other solution, please?


delete flag offensive retag edit


Hello, have you tried to use DefaultTreeModel and configure it to treat the zero size of children node as a leaf node? Please refer reference.

RaymondChao ( 2014-08-08 02:43:21 +0800 )edit

If I add a new leaf with zero size of children, this leaf is shown as a category node(with arrow), but I want to have a leaf node(without arrow) and only after I adding a child to it, to convert the leaf node into a category node.

ZKL ( 2014-08-08 08:19:03 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2014-08-09 15:01:27 +0800

Darksu gravatar image Darksu
1991 1 4

Hello ZKL,

A solution to your issue could be the following:

  1. Store into a collection all the categories, and leaf nodes (I would create a model that would store an id, parentId, name, and position)

  2. Insert into the collection the new object (when the user presses the add button. Here you will have to pay attention to add it to the correct position in the collection)

  3. Re-render the tree using the updated collection (If the object has children, then treat it as a category node, and if the object does not have children then treat it as a leaf node)

Hope this pattern will help.

Best Regards,


link publish delete flag offensive edit

answered 2014-08-11 08:32:16 +0800

ZKL gravatar image ZKL
3 1

Hello Darksu,

If I understand you correctly, Your suggestion is to refresh the tree after each node insertion, this means that we should to save the current state of tree('open' property of each node), because: after refresh, the tree is shown by default with all nodes closed.

I found the 'insertBefore' method of Treeitem, it seems that can convert a leaf treeitem into a category treeitem, but without children nodes. Does someone used this method in a tree? Can be achieved my objective using 'insertBefore' method?

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2014-08-07 08:47:03 +0800

Seen: 40 times

Last updated: Aug 11 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More