Link to home
Start Free TrialLog in
Avatar of saidreddy
saidreddy

asked on

JTree node selection

Hi ,
  I have created a tree structure as follows and i have made the root element set to invisible.
so the structure is as follows

121212
-----12
-----13
-----14
141414
------111
------112

Now when i click 121212 then i should display a dialog using thenode value and when i click 12or 13 or 14 i should display another dialog.

Now how can i tell my program which node i am clicking i mean the parent(121212) or the child(12 or 13 or 14).
I have tried the methods for the treenode like isLeaf, getChildcount and others but could not find a solution.

Can anybody help me .

Thanks in advance.

regards,
said

Avatar of zzynx
zzynx
Flag of Belgium image

Try

    private void theTreeMousePressed(java.awt.event.MouseEvent evt) {
        if (SwingUtilities.isLeftMouseButton(evt)) {
            DefaultMutableTreeNode nodeClickedOn = getSelected();
        }
    }

    public DefaultTreeNode getSelection() {
        if (theTree.getSelectionPath()==null) return null;
        return (DefaultTreeNode)theTree.getSelectionPath().getLastPathComponent();
    }
Of course in conjunction with

        yourTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                theTreeMousePressed(evt);
            }
        });
Better:

yourTree.addTreeSelectionListener( new TreeSelectionListener () {

    public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode nodeClickedOn = getSelected();
        if (nodeClickedOn!=null) {
             // Do whatever you want to do
        }
    }

});
Sorry, there's a typo in getSelection():

    public DefaultMutableTreeNode TreeNode getSelection() {
        if (theTree.getSelectionPath()==null) return null;
        return (DefaultMutableTreeNode)theTree.getSelectionPath().getLastPathComponent();       // <<<<< DefaultMUTABLETreeNode
    }
Avatar of saidreddy
saidreddy

ASKER

Hi all,
  Thanks for your replies.
But what i was expecting was if i click the sub node then i should know that i have clicked the subnode and if i have clicked the parent node then i should now i have clicked parent node.
As before i have said i have to display a dialog basing on the value and the tree consists of many nodes and child nodes and i have to know whether it is a child node or a parent node which i have clicked.


thanks once again

regards,
said
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tree.addTreeSelectionListener( new TreeSelectionListener ()
{
    public void valueChanged(TreeSelectionEvent e)
    {
        TreePath selected = e.getNewLeadSelectionPath();
        if (selected!=null)
        {
           TreeNode node = selected.getLastPathComponent();
           if (node.isLeaf())
           {
               // leaf node selected
           }
           else
           {
               // non-leaf node selected
           }
        }
    }
});
Thanks for accepting