JTree : Getting the parent of a selected node.

I have a JTree that could be visualised as follows :

root

|-- PersonA

||-- AddrA1

||-- AddrA2

|-- PersonB

|-- AddrB1

|-- AddrB2

|-- AddrB3

My tree model is identical as the one that we could find from Swing tutorial,

GenealogyModel.java

I would like to know how to find the parent of a selected node. If a user

select, say, AddrA2, the parent

is PersonA. If AddrB3, it's PersonB.

I tried to use the following code :

tree.addTreeSelectionListener(new TreeSelectionListener(){

publicvoid valueChanged(TreeSelectionEvent e){

Object o = tree.getLastSelectedPathComponent();

if (oinstanceof Address){

// Find the parent of the selected Address

TreePath path = tree.getSelectionPath();

log.info("==> Path is " + path.toString());

DefaultMutableTreeNode node = (DefaultMutableTreeNode)o;

DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node.getParent();

Person p = (Parent)parent.getUserObject();

}

}

});

but it throws an ClassCastException, saying that Address class cannot be cast to DefaultMutableTreeNode.

Anyone knows how to get the parent of a selectec node ?

Thanks for any help/suggestions.

[1679 byte] By [nusaa] at [2007-11-27 10:10:59]
# 1

TreeNode trnodTemp = (TreeNode) tree.getLastSelectedPathComponent();

trnodTemp.getParent();

check this code

dayanandabva at 2007-7-28 15:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

Nop, still got ClassCastException Address cannot be cast to javax.swing.tree.TreeNode.

Any more idea ?

Thanks.

nusaa at 2007-7-28 15:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 3

I'd read the tutorials on JTree's a little more....

In your listener you check if o is an instance off Address and then you try to cast it to a DefaultMutableTreeNode...

c0demonk3ya at 2007-7-28 15:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 4

I don't get what you mean c0demonk3y, could you please specify ?

nusaa at 2007-7-28 15:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 5

In your listener you create an Object o from the TreeSelecitonEvent as follows: -

Object o = tree.getLastSelectedPathComponent();

if (o instanceof Address) {

//...

}

You then have another if statement checking that o is an instance of Address... then inside this if statement you try to cast the Address object o to a DefaultMutableTreeNode.

That is why you are getting a class cast exception

c0demonk3ya at 2007-7-28 15:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 6

OK, so how can I get the parent of a selected node ?

nusaa at 2007-7-28 15:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 7

Anyone please ?

nusaa at 2007-7-28 15:10:06 > top of Java-index,Desktop,Core GUI APIs...
# 8

TreePath path = JTree.getSelectionPath();

int count = path.getPathCount();

Object node = path.getPathComponent(count - 2);

tjacobs01a at 2007-7-28 15:10:06 > top of Java-index,Desktop,Core GUI APIs...
# 9

Thanks tjacobs01, that works.

nusaa at 2007-7-28 15:10:06 > top of Java-index,Desktop,Core GUI APIs...