need to insert a parent in a jtree

M

MikeB

I find lots of material on appending items to the end of a jtree but
how do I insert a node and make every thing to the left of it it's
children.

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
root.add(child1);
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
root.add(child2);

Let's say that the above code created my tree, but later on I want to
insert a new node after child1 and make child2 the child of the newly
inserted node. So the before and after will look like the below
example.

before:
root
+ child1
+ child2

after:
root
+ child1
+ newNode
+ child2
 
T

Thomas Weidenfeller

MikeB said:
I find lots of material on appending items to the end of a jtree

Surprising, because this is really not a a case which is much more
common than others.

But before going into details, you are actually not appending anything
to a JTree. A JTree is just a visual representation of you model. You
change the model, and let the JTree pick up the changes and display the
new data. The distinction is important, as I will point out in a second.
Let's say that the above code created my tree, but later on I want to
insert a new node after child1 and make child2 the child of the newly
inserted node. So the before and after will look like the below
example.

before:
root
+ child1
+ child2

after:
root
+ child1
+ newNode
+ child2

In three steps:

Remove child2 from its parent
Add child2 as child to newNode
Insert newNode at the desired position

Other sequences are also possible (e.g. first adding newNode, then
removing child2, than adding child2 to newNode).

The important thing is that the JTree is properly informed about the
changes in the model once the changes are done. You can either

* perform all operations via the TreeModel. Which, if correctly
implemented, will inform the JTree, or you can

* rearrange the nodes and notify the JTree afterwards.

Rearranging the nodes and notifying the JTree afterwards has the
advantage that you can limit the number of notifications which are fired
to the JTree. If you go via the model, you probably trigger two events
(for the remove and the insert).

So a code sequence which just fires one event would look something like

DefaultMutableTreeNode parent, child1, child2, newNode;
DefaultTreeModel tm;

int ndx = parent.getIndex(child2); // remember where to insert
parent.remove(pos);
newNode.insert(child2, 0);
parent.insert(newNode, pos); // reinsert at old pos. of child2

// let the model inform the JTree about changes below parent
tm.nodeStructureChanged(parent);


/Thomas
 
M

MikeB

Thanks Thomas for the quick response. I need some clarification about
your example, parent.remove(pos); is pos = ndx?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

change icon in jtree 0
DOM extension of JTree 2
Programmatically removing nodes from a JTree 2
JTree oddity... 5
JTable within JTree 1
JTree model update problem 3
JCheckBox in a JTree 1
Filtered JTree 0

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top