Skip to main content

dnd with trees

7 replies [Last post]
lizzygrrrl
Offline
Joined: 2006-02-17
Points: 0

Hi,

I'm working an a tree that allows drag and drop. Whenever I move a child node into a another parent's node (that is, not its owv parent), that area of the JTree doesn't render - at all. Further, scrolling in the area tends to cause a NullPointerException and my guess is that this is coming from the call getUI{} in JTree.java.

The only method that makes this work is calling reload() against the whole model but that makes all the folders close. I have tried calling nodeStructureChanged on only the folders that change (there is an insert followed by a delete) but so far it does not work.

Any ideas?

Thank you, Elizabeth

Reply viewing options

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

On Mon, Nov 24, 2003 at 09:58:06AM -0500, swing-feedback@javadesktop.org wrote:
> Hi,
>
> I'm working an a tree that allows drag and drop. Whenever I move a
> child node into a another parent's node (that is, not its owv
> parent), that area of the JTree doesn't render - at all. Further,
> scrolling in the area tends to cause a NullPointerException and my
> guess is that this is coming from the call getUI{} in JTree.java.

Are you sending out the appropriate TreeModelListener notification?
If you are using DefaultMutableTreeNodes and manipulating the nodes
outside of the DefaultTreeModel than most likely you are not sending
out notification which can cause these sort of problems.

-Scott

lizzygrrrl
Offline
Joined: 2006-02-17
Points: 0

Hi Scott,

I checked over my code and did find an error but not one that corrects this issue for some reason. I also added an option on the menubar to insert a node and it is doing the same thing so it probably is the TreeModel.

Briefly, here is what I do when I add a node into the model:

1) Insert the node proper into its parent.
2) I then fire off TreeModelEvent that calls treeNodesInserted() on each registered listener passing the four-value-constructor generated TreeModelEvent.

After I do this the whole frame goes blank and I can scroll down into the next pag but as I scroll back up into the modified portion I receive a NullPointerException.

As far as I can tell I'm doing things correctly but .. ?!?!

Thank you,
Elizabeth

Scott Violet

On Mon, Nov 24, 2003 at 03:21:24PM -0500, swing-feedback@javadesktop.org wrote:
> Hi Scott,
>
> I checked over my code and did find an error but not one that corrects this issue for some reason. I also added an option on the menubar to insert a node and it is doing the same thing so it probably is the TreeModel.
>
> Briefly, here is what I do when I add a node into the model:
>
> 1) Insert the node proper into its parent.
> 2) I then fire off TreeModelEvent that calls treeNodesInserted() on each registered listener passing the four-value-constructor generated TreeModelEvent.
>
> After I do this the whole frame goes blank and I can scroll down into the next pag but as I scroll back up into the modified portion I receive a NullPointerException.
>
> As far as I can tell I'm doing things correctly but .. ?!?!

I can only think the event you're creating doesn't have the correct
arguments. Do you have a test case?

-Scott

lizzygrrrl
Offline
Joined: 2006-02-17
Points: 0

It is pretty basic:
[code]
public void insertNodeInto(TreeNode newChild, TreeNode parent, int index) {
if(!parent.isLeaf()) {
// note that this is implementation specific as
// all our non-leaf nodes extend Vector
Vector p = (Vector) parent;
p.insertElementAt(newChild, index);
nodesWereInserted(parent, new int[] { index } );
}
}

public void nodesWereInserted(TreeNode node, int[] indices) {
fireTreeNodesInserted(this, getPathToRoot(node), indices, getChildrenOfNode(node, indices));
}

private Object[] getChildrenOfNode(TreeNode node, int[] childIndices) {
Object[] rv = new Object[childIndices.length];
for(int i = 0; i < childIndices.length; i++) {
rv[i] = node.getChildAt(childIndices[i]);
}
return rv;
}

public TreePath getPathToRoot(TreeNode node) {
return new TreePath(getPathToRoot(node, 0));
}

protected Object[] getPathToRoot(TreeNode node, int level) {
if(node == null)
return new Object[level];
Object[] rv = getPathToRoot(node.getParent(), level + 1);
rv[rv.length - level - 1] = node;
return rv;
}

protected void fireTreeNodesInserted(Object source, TreePath path, int[] childIndices, Object[] children) {
fireTreeModelEvent(source, path, childIndices, children, NODES_INSERTED);
}

private void fireTreeModelEvent(Object source, TreePath path, int[] childIndices, Object[] children, int typeOfEvent) {
TreeModelEvent e = new TreeModelEvent(source, path, childIndices, children);
fireTreeModelEvent(e, typeOfEvent);
}

// fireTreeModelEvent is pretty simple
[/code]
Thank you for your help, Elizabeth

Message was edited by: lizzygrrrl

Scott Violet

On Tue, Nov 25, 2003 at 04:41:45AM -0500, swing-feedback@javadesktop.org wrote:
> It is pretty basic:
[code]
public void insertNodeInto(TreeNode newChild, TreeNode parent, int index) {
if(!parent.isLeaf()) {
// note that this is implementation specific as
// all our non-leaf nodes extend Vector
Vector p = (Vector) parent;
[/code]

It looks like your TreeNodes are subclasses of Vector, is that true?
If so, do you override hashCode and equals? Swing's JTree
implementation places the nodes in a Map, so that it is entirely
possible you are running into problems because of how Vector
implements hashCode and equals.

-Scott

lizzygrrrl
Offline
Joined: 2006-02-17
Points: 0

Wow!!! That made all the difference in the world! Thank you soooo much!!

I based the new implementation of equals() and hashCode() on System.identityHashCode() - does this seem prudent to you?

Elizabeth

Scott Violet

On Tue, Nov 25, 2003 at 01:42:47PM -0500, swing-feedback@javadesktop.org wrote:
> Wow!!! That made all the difference in the world! Thank you soooo much!!
>
> I based the new implementation of equals() and hashCode() on
> System.identityHashCode() - does this seem prudent to you?

Hard to say, but that's typically a good thing for trees.

-Scott