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

saidreddyAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
Or you ask that to the userObject stored in your node object:

yourTree.addTreeSelectionListener( new TreeSelectionListener () {

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

});

Or if you don't store user objects in it:

yourTree.addTreeSelectionListener( new TreeSelectionListener () {

    public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode nodeClickedOn = getSelected();
        if (nodeClickedOn!=null) {
             // Do whatever you want to do
             if ( (DefaultMutableTreeNode)nodeClickedOn.getParent().isRoot() ) {   // its parent node is the root
                  // parent node
             }
             else {
                 // child node
             }
        }
    }

});
0
 
zzynxSoftware engineerCommented:
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();
    }
0
 
zzynxSoftware engineerCommented:
Of course in conjunction with

        yourTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                theTreeMousePressed(evt);
            }
        });
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
zzynxSoftware engineerCommented:
Better:

yourTree.addTreeSelectionListener( new TreeSelectionListener () {

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

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

    public DefaultMutableTreeNode TreeNode getSelection() {
        if (theTree.getSelectionPath()==null) return null;
        return (DefaultMutableTreeNode)theTree.getSelectionPath().getLastPathComponent();       // <<<<< DefaultMUTABLETreeNode
    }
0
 
saidreddyAuthor Commented:
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
0
 
objectsCommented:
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
           }
        }
    }
});
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
All Courses

From novice to tech pro — start learning today.