JTree display update issue with new nodes



I have a problem with the following code. The code is supposed to create a
new node and add it as a child to another node in a JTree. This is indeed
happening but the tree is only updating the display and selecting the new
node the first time a node is added. For each subsequent child node, it
appears as though the child node is added successfully but it is just not
appearing in the tree. If I check getChildCount() on the parent node after
each child is added it reveals the correct number so it seems to be a
display issue. Could someone please point out the mistake in the following
code?

This code exists in a control class (not in the JTree itself). The variable
parentNode is a DefaultMutableTreeNode passed in as a parameter:

DefaultMutableTreeNode childNode = new DefaultMutableTreeNode();
parentNode.add(childNode);
tree.setSelectionPath(new TreePath(childNode.getPath()));
MyTreeModel model = (MyTreeModel)tree.getModel();
model.nodesWereInserted(parentNode, new int[] {parentNode.getChildCount() -
1});
tree.repaint();

This is the tree model:

public class MyTreeModel extends DefaultTreeModel
{
private DefaultMutableTreeNode root;
private ArrayList<TreeModelListener> listeners = new
ArrayList<TreeModelListener>();

public MyTreeModel(DefaultMutableTreeNode root)
{
super(root);
this.root = root;
}

public void addTreeModelListener(TreeModelListener l)
{
listeners.add(l);
}

public Object getChild(Object parent, int index)
{
return ((DefaultMutableTreeNode)parent).getChildAt(index);
}

public int getChildCount(Object parent)
{
return ((DefaultMutableTreeNode)parent).getChildCount();
}

public int getIndexOfChild(Object parent, Object child)
{
return
((DefaultMutableTreeNode)parent).getIndex((DefaultMutableTreeNode)child);
}

public Object getRoot()
{
return root;
}

public boolean isLeaf(Object node)
{
return ((DefaultMutableTreeNode)node).isLeaf();
}

public void removeTreeModelListener(TreeModelListener l)
{
listeners.remove(l);
}

public void valueForPathChanged(TreePath path, Object newValue)
{
}
}

--
And loving it,

qu0ll
______________________________________________
qu0llSixFour@xxxxxxxxx
(Replace the "SixFour" with numbers to email)


.



Relevant Pages

  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)