This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to add child node on right click of a node to a JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add child node on right click of a node to a JTree" Watch "How to add child node on right click of a node to a JTree" New topic
Author

How to add child node on right click of a node to a JTree

Shweta Baranwal
Greenhorn

Joined: Jun 19, 2008
Posts: 18
Hi,


I have to add a child new node on right click of a parent node to JTree. The JTree should work same as tree works while we explore things in myComputer.
Please tell me if it is possible or not? If yes then please help me to do this...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

It works if you use a DefaultTreeModel, with TreeNodes.

Upon right clicking (use a MouseListener and its isPopupTrigger method), you create a new TreeNode (usually DefaultMutableTreeNode), retrieve the model from the JTree, and call its insertNodeInto method:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: How to add child node on right click of a node to a JTree