Skip to main content

Problem with JTree refresh on display

1 reply [Last post]
AtWorkZonie
Offline
Joined: 2006-02-17
Points: 0

I am having trouble displaying a JTree with newly added nodes.
I have a JTree, inside a JPanel, JScrollPane and JSplitPane.
I create the JTree with a single root node which displays correctly.
I update the JTree with new children which displays correctly.
Using the same exact routine as above - I add additional new children to the root, the new children do not display in the JTree.
I have confirmed that I am using the correct tree and root node in debug that the tree/node objects has been correctly updated. It is just not displaying the new children to the root.
I can expand and collapse the tree which does not show the new children, just the original populated root.
After the root has been updated, I have tried a number of methods to resolve: tree/jpanel/jscrollpane/jsplitpane .revalidate, invalidate, repaint, etc.
I am using v1.4.1.
What am I missing here?
Thanks in advance.

The routine does the following.

public void addNewNode(JTree tree)
{
TreeModel model = tree.getModel();
SummarySelectNode root = (SummarySelectNode) model.getRoot();

// Create Primary Node add to root
// SummarySelectNode is extended from DefaultMutableTreeNode

SummarySelectNode pNode = new SummarySelectNode();
root.add(pNode);
......

((DefaultTreeModel) tree.getModel()).nodeChanged(root);
tree.revalidate();

for (int i = 0; i < tree.getRowCount(); i++)
tree.expandRow(i);

tree.revalidate();
tree.repaint();

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Scott Violet

On Wed, Oct 22, 2003 at 07:25:03PM -0400, swing-feedback@javadesktop.org wrote:
> I am having trouble displaying a JTree with newly added nodes.
> I have a JTree, inside a JPanel, JScrollPane and JSplitPane.
> I create the JTree with a single root node which displays correctly.
> I update the JTree with new children which displays correctly.
> Using the same exact routine as above - I add additional new children to the root, the new children do not display in the JTree.
> I have confirmed that I am using the correct tree and root node in debug that the tree/node objects has been correctly updated. It is just not displaying the new children to the root.
> I can expand and collapse the tree which does not show the new children, just the original populated root.
> After the root has been updated, I have tried a number of methods to resolve: tree/jpanel/jscrollpane/jsplitpane .revalidate, invalidate, repaint, etc.
> I am using v1.4.1.
> What am I missing here?
> Thanks in advance.
>
> The routine does the following.
>
> public void addNewNode(JTree tree)
> {
> TreeModel model = tree.getModel();
> SummarySelectNode root = (SummarySelectNode) model.getRoot();
>
> // Create Primary Node add to root
> // SummarySelectNode is extended from DefaultMutableTreeNode
>
> SummarySelectNode pNode = new SummarySelectNode();
> root.add(pNode);
> ......
>
> ((DefaultTreeModel) tree.getModel()).nodeChanged(root);
> tree.revalidate();
>
> for (int i = 0; i < tree.getRowCount(); i++)
> tree.expandRow(i);
>
> tree.revalidate();
> tree.repaint();

AtWorkZonie,

nodeChanged is used to indicate the visual representation of a node
has some how changed, it should not be used to indicate the structure
was changed. I would suggest that rather than using
DefaultMutableTreeNode.add you use DefaultTreeModel.insertNodeInto.
Additionally you do not need to invoke repaint/revalidate, the
listeners that JTree installs on the TreeModel will handle all that
for you.

-Scott