Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-05-2008, 08:48 AM
Member
 
Join Date: Jan 2008
Posts: 5
Kabiraa is an unknown quantity at this point
how to create Popup Menu with Sub Menu while right-clicking the JTree Node??
Hello frnz...

I wanted to know how to create a Popup Menu along with Sub Menu when i right-click the perticular Node of JTree...

I m using NetBeans 6.0 with jre 6..

I want a simple application in which there is a tree with the root "Family". I want to create child nodes using popup menu. so i need that when i right-click the "Family" node, a popup menu appears with item "Add". This item shud have sub menus-"Add Child", "Add Sublings".

I m new to Java, so pls help me..


thnx in advance..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-21-2008, 08:10 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; public class TreePopup { JTree tree = new JTree(); JPopupMenu popup; public TreePopup() { popup = new JPopupMenu(); popup.setInvoker(tree); PopupHandler handler = new PopupHandler(tree, popup); popup.add(getMenuItem("add child", handler)); popup.add(getMenuItem("add sibling", handler)); } private JMenuItem getMenuItem(String s, ActionListener al) { JMenuItem menuItem = new JMenuItem(s); menuItem.setActionCommand(s.toUpperCase()); menuItem.addActionListener(al); return menuItem; } private JScrollPane getTreeComponent() { tree.add(popup); expand(new TreePath(tree.getModel().getRoot())); return new JScrollPane(tree); } private void expand(TreePath path) { TreeNode node = (TreeNode)path.getLastPathComponent(); if (node.getChildCount() >= 0) { java.util.Enumeration e = node.children(); while(e.hasMoreElements()) { TreeNode n = (TreeNode)e.nextElement(); expand(path.pathByAddingChild(n)); } } tree.expandPath(path); } public static void main(String[] args) { TreePopup test = new TreePopup(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getTreeComponent()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class PopupHandler implements ActionListener { JTree tree; JPopupMenu popup; Point loc; public PopupHandler(JTree tree, JPopupMenu popup) { this.tree = tree; this.popup = popup; tree.addMouseListener(ma); } public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); TreePath path = tree.getPathForLocation(loc.x, loc.y); //System.out.println("path = " + path); //System.out.printf("loc = [%d, %d]%n", loc.x, loc.y); if(ac.equals("ADD CHILD")) addChild(path); if(ac.equals("ADD SIBLING")) addSibling(path); } private void addChild(TreePath path) { DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getLastPathComponent(); int count = parent.getChildCount(); DefaultMutableTreeNode child = new DefaultMutableTreeNode("child " + count); DefaultTreeModel model = (DefaultTreeModel)tree.getModel(); model.insertNodeInto(child, parent, count); } private void addSibling(TreePath path) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node.getParent(); int count = parent.getChildCount(); DefaultMutableTreeNode child = new DefaultMutableTreeNode("child " + count); DefaultTreeModel model = (DefaultTreeModel)tree.getModel(); model.insertNodeInto(child, parent, count); } private MouseListener ma = new MouseAdapter() { private void checkForPopup(MouseEvent e) { if(e.isPopupTrigger()) { loc = e.getPoint(); popup.show(tree, loc.x, loc.y); } } public void mousePressed(MouseEvent e) { checkForPopup(e); } public void mouseReleased(MouseEvent e) { checkForPopup(e); } public void mouseClicked(MouseEvent e) { checkForPopup(e); } }; }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-03-2008, 07:55 AM
Member
 
Join Date: Jan 2008
Posts: 5
Kabiraa is an unknown quantity at this point
thnx hardwired..

bt still i need somethin different..i need tht the main menu shud be "Insert", & it shud have the sub menus like "add child" & "add sibling"...

in ur code, it directly shows the two submenus instead of showing them under the main menu "Insert"..

so cud u jss let me know how to this.??
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-03-2008, 08:12 AM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Code:
public TreePopup() { popup = new JPopupMenu(); popup.setInvoker(tree); PopupHandler handler = new PopupHandler(tree, popup); JMenu menu = new JMenu("insert"); popup.add(menu); menu.add(getMenuItem("add child", handler)); menu.add(getMenuItem("add sibling", handler)); }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-03-2008, 08:22 AM
Member
 
Join Date: Jan 2008
Posts: 5
Kabiraa is an unknown quantity at this point
still u dont get me..

i need the menu "Insert" only..when we click "Insert" it shud display two submenus "add child" & "add sibling"..

ne waz, I done it by myself..

here is the code:

Code:
public TreePopup() { popup = new JPopupMenu(); popup.setInvoker(tree); PopupHandler handler = new PopupHandler(tree, popup); JMenu menu = new JMenu("insert"); menu.add(getMenuItem("add child", handler)); menu.add(getMenuItem("add sibling", handler)); popup.add(menu); }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-07-2008, 10:42 AM
Member
 
Join Date: Jan 2008
Posts: 5
Kabiraa is an unknown quantity at this point
At this time I am able to open the popup menu by right clicking on the JTree..

Now, I have one more problem..
--> I want that when I right click any of the node of JTree (except root node), it should display the popup menu as it is.
--> But when I right click on the root node, then it should not display the "Add Sibling" submenu or make it disable.
--> When I right click on the blank area of JTree (not on any of the nodes) then it should not display the popup menu at all..

so, is it possible to do it in this manner.??

pls help me out..
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-08-2008, 10:58 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; public class TreePopup { private JScrollPane getTreeComponent() { JTree tree = new JTree(); PopupHandler handler = new PopupHandler(tree); tree.add(handler.getPopup()); expand(tree, new TreePath(tree.getModel().getRoot())); return new JScrollPane(tree); } private void expand(JTree tree, TreePath path) { TreeNode node = (TreeNode)path.getLastPathComponent(); if (node.getChildCount() >= 0) { java.util.Enumeration e = node.children(); while(e.hasMoreElements()) { TreeNode n = (TreeNode)e.nextElement(); expand(tree, path.pathByAddingChild(n)); } } tree.expandPath(path); } public static void main(String[] args) { TreePopup test = new TreePopup(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getTreeComponent()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class PopupHandler implements ActionListener, PopupMenuListener { JTree tree; JPopupMenu popup; JMenuItem item; boolean overRoot = false; Point loc; public PopupHandler(JTree tree) { this.tree = tree; popup = new JPopupMenu(); popup.setInvoker(tree); JMenu menu = new JMenu("insert"); popup.add(menu); menu.add(getMenuItem("add child")); menu.add(item = getMenuItem("add sibling")); tree.addMouseListener(ma); popup.addPopupMenuListener(this); } private JMenuItem getMenuItem(String s) { JMenuItem menuItem = new JMenuItem(s); menuItem.setActionCommand(s.toUpperCase()); menuItem.addActionListener(this); return menuItem; } public JPopupMenu getPopup() { return popup; } public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); TreePath path = tree.getPathForLocation(loc.x, loc.y); //System.out.println("path = " + path); //System.out.printf("loc = [%d, %d]%n", loc.x, loc.y); if(ac.equals("ADD CHILD")) addChild(path); if(ac.equals("ADD SIBLING")) addSibling(path); } private void addChild(TreePath path) { DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getLastPathComponent(); int count = parent.getChildCount(); DefaultMutableTreeNode child = new DefaultMutableTreeNode("child " + count); DefaultTreeModel model = (DefaultTreeModel)tree.getModel(); model.insertNodeInto(child, parent, count); } private void addSibling(TreePath path) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node.getParent(); int count = parent.getChildCount(); DefaultMutableTreeNode child = new DefaultMutableTreeNode("child " + count); DefaultTreeModel model = (DefaultTreeModel)tree.getModel(); model.insertNodeInto(child, parent, count); } private MouseListener ma = new MouseAdapter() { private void checkForPopup(MouseEvent e) { if(e.isPopupTrigger()) { loc = e.getPoint(); TreePath path = tree.getPathForLocation(loc.x, loc.y); //System.out.printf("path = %s%n", path); if(path == null) { e.consume(); return; } TreeNode root = (TreeNode)tree.getModel().getRoot();; overRoot = path.getLastPathComponent() == root; popup.show(tree, loc.x, loc.y); } } public void mousePressed(MouseEvent e) { checkForPopup(e); } public void mouseReleased(MouseEvent e) { checkForPopup(e); } public void mouseClicked(MouseEvent e) { checkForPopup(e); } }; public void popupMenuWillBecomeVisible(PopupMenuEvent e) { item.setVisible(!overRoot); } public void popupMenuCanceled(PopupMenuEvent e) {} public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {} }
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-09-2008, 08:54 AM
Member
 
Join Date: Jan 2008
Posts: 5
Kabiraa is an unknown quantity at this point
thnx man..
it really helped me a lot..
thnx..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
menu in top left always sschwinghammer New To Java 2 02-06-2008 03:39 PM
Drop down menu BenNeiderlander New To Java 3 02-05-2008 08:35 AM
JTree Programmatic Node Expansion and Selection Probelm hemanthjava AWT / Swing 1 01-17-2008 07:36 AM
Using SWT Menu Java Tip Java Tips 0 01-09-2008 01:04 PM
how to create a menu bar in java tommy New To Java 1 08-05-2007 08:43 AM


All times are GMT +3. The time now is 01:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org