Expanding Tree to Inserted Node

B

Barb

The following code is supposed to expand a tree once a new node has
been added. Unfortunately it does not and I do not know why. Can
anyone help?

if (editPolicy == Constants.Gen_AsNew) {
// For new, first build a new Node Object with
// all default blank values
// If saved, then add to tree
navTreeModel.insertNodeInto(
treeNode, parentTreeNode,
parentTreeNode.getChildCount()
);

// XXX JS 16/1/3 this expansion doesn't work - don't know why !

// Get the index of the parentNode, and
// then expand the tree for the current index

int i =
navTreeModel.getChildCount(this.parentTreeNode) - 1;
TreeNode[] theNodes = navTreeModel.getPathToRoot(
(TreeNode) navTreeModel.getChild(
this.parentTreeNode, i
)
);
TreePath theNodePath = new TreePath(theNodes);
navTree.expandPath(theNodePath);
navTree.repaint();

Any help would be greatly appreciated.

Thanks Barb
 
?

=?ISO-8859-1?Q?Andree_Gro=DFe?=

Barb said:
The following code is supposed to expand a tree once a new node has
been added. Unfortunately it does not and I do not know why. Can
anyone help?
...

Sample source:

from MyTreeModel:

public TreePath insert
(DefaultMutableTreeNode node, DefaultMutableTreeNode parent)
{
this.insertNodeInto(node, parent, parent.getChildCount());
this.nodeStructureChanged(parent);
return new TreePath(this.getPathToRoot(node));
}

within GUI class which contains the JTree:

private JTree jTree = new JTree(new MyTreeModel());

...
TreePath newPath = ((MyTreeModel)jTree.getModel()).insert(newNode, parent);
visit(newPath);
...

private void visit (TreePath path) {
jTree.expandPath(path);
jTree.setSelectionPath(path);
jTree.scrollRectToVisible(jTree.getPathBounds(path));
}

HTH A.G.
 
C

Christian Kaufhold

Andree Große said:
Sample source:

from MyTreeModel:

public TreePath insert
(DefaultMutableTreeNode node, DefaultMutableTreeNode parent)
{
this.insertNodeInto(node, parent, parent.getChildCount());



this.nodeStructureChanged(parent);

Useless, even harmful.



Christian
 
C

Christian Kaufhold

Andree Große said:

Nothing about the structure of "parent" has changed. There is no reason
to tell that listeners that it has.

JTree will deselect/collapse all descendants of parent on receiving such
an event.



Christian
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top