[JTree] click on a node or a leaf

A

Alexandre Jaquet

Hi I would like to know how know if a user click on a node or a leaf
thanks.
 
T

Thomas Weidenfeller

Alexandre Jaquet said:
Hi I would like to know how know if a user click on a node or a leaf
thanks.

You can ask the node if it is a leave or not. See the API documentation
of TreeNode.

/Thomas
 
M

Matthew Zimmer

Alexandre said:
Hi I would like to know how know if a user click on a node or a leaf
thanks.

Do you mean how to know when something is selected or if the something
selected is a node or not? If you mean the first, use this:

myTree.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent evt)
{
TreePath path = this.getSelectionPath();
if (path != null)
{
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)path.getLastPathComponent();
if (node.isLeaf())
{
// do child stuff
}
else
{
// do parent stuff
}

}
}
});


Hope that helps.
Matthew
 

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

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top