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
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
Of course in conjunction with
yourTree.addMouseListener( new java.awt.event.MouseAdapte r() {
public void mousePressed(java.awt.even t.MouseEve nt evt) {
theTreeMousePressed(evt);
}
});
yourTree.addMouseListener(
public void mousePressed(java.awt.even
theTreeMousePressed(evt);
}
});
Better:
yourTree.addTreeSelectionL istener( new TreeSelectionListener () {
public void valueChanged(TreeSelection Event e) {
DefaultMutableTreeNode nodeClickedOn = getSelected();
if (nodeClickedOn!=null) {
// Do whatever you want to do
}
}
});
yourTree.addTreeSelectionL
public void valueChanged(TreeSelection
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)th eTree.getS electionPa th().getLa stPathComp onent(); // <<<<< DefaultMUTABLETreeNode
}
public DefaultMutableTreeNode TreeNode getSelection() {
if (theTree.getSelectionPath(
return (DefaultMutableTreeNode)th
}
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
tree.addTreeSelectionListe ner( new TreeSelectionListener ()
{
public void valueChanged(TreeSelection Event e)
{
TreePath selected = e.getNewLeadSelectionPath( );
if (selected!=null)
{
TreeNode node = selected.getLastPathCompon ent();
if (node.isLeaf())
{
// leaf node selected
}
else
{
// non-leaf node selected
}
}
}
});
{
public void valueChanged(TreeSelection
{
TreePath selected = e.getNewLeadSelectionPath(
if (selected!=null)
{
TreeNode node = selected.getLastPathCompon
if (node.isLeaf())
{
// leaf node selected
}
else
{
// non-leaf node selected
}
}
}
});
Thanks for accepting
private void theTreeMousePressed(java.a
if (SwingUtilities.isLeftMous
DefaultMutableTreeNode nodeClickedOn = getSelected();
}
}
public DefaultTreeNode getSelection() {
if (theTree.getSelectionPath(
return (DefaultTreeNode)theTree.g
}