Results 1 to 8 of 8
  1. #1
    Kabiraa is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Question 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..

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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); }
        };
    }

  3. #3
    Kabiraa is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    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.??

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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));
        }

  5. #5
    Kabiraa is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    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);
        }

  6. #6
    Kabiraa is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    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..

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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) {}
    }

  8. #8
    Kabiraa is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

Similar Threads

  1. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 08:23 AM
  2. menu in top left always
    By sschwinghammer in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 03:39 PM
  3. Drop down menu
    By BenNeiderlander in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 08:35 AM
  4. Using SWT Menu
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 01:04 PM
  5. how to create a menu bar in java
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •