Results 1 to 8 of 8
- 03-05-2008, 07:48 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 12
- Rep Power
- 0
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..
- 03-21-2008, 07:10 PM #2Java 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); } }; }
- 05-03-2008, 06:55 AM #3
Member
- Join Date
- Jan 2008
- Posts
- 12
- Rep Power
- 0
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.??
- 05-03-2008, 07:12 AM #4Java 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)); }
- 05-03-2008, 07:22 AM #5
Member
- Join Date
- Jan 2008
- Posts
- 12
- Rep Power
- 0
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:
Java 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); }
- 05-07-2008, 09:42 AM #6
Member
- Join Date
- Jan 2008
- Posts
- 12
- Rep Power
- 0
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..
- 05-08-2008, 09:58 PM #7Java 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) {} }
- 05-09-2008, 07:54 AM #8
Member
- Join Date
- Jan 2008
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
JTree Programmatic Node Expansion and Selection Probelm
By hemanthjava in forum AWT / SwingReplies: 3Last Post: 01-16-2013, 07:23 AM -
menu in top left always
By sschwinghammer in forum New To JavaReplies: 2Last Post: 02-06-2008, 02:39 PM -
Drop down menu
By BenNeiderlander in forum New To JavaReplies: 3Last Post: 02-05-2008, 07:35 AM -
Using SWT Menu
By Java Tip in forum Java TipReplies: 0Last Post: 01-09-2008, 12:04 PM -
how to create a menu bar in java
By tommy in forum New To JavaReplies: 1Last Post: 08-05-2007, 07:43 AM
Bookmarks