当前位置: 代码迷 >> J2SE >> java 中在树节点下右击弹出菜单的方法!
  详细解决方案

java 中在树节点下右击弹出菜单的方法!

热度:7281   发布时间:2013-02-25 00:00:00.0
java 中在树节点上右击弹出菜单的方法!!!
如何在JTree控件上的指定的节点上右击就弹出菜单,是在节点上右击弹出,不是在树的任一个地方!!!!最好是给一个简单的例子!!!谢谢!!!

------解决方案--------------------------------------------------------
Java code
JTree fTree = new JTree();        fTree.addMouseListener(new MouseListener() {            @Override            public void mouseClicked(MouseEvent e) {            }            @Override            public void mouseEntered(MouseEvent e) {            }            @Override            public void mouseExited(MouseEvent e) {            }            @Override            public void mousePressed(MouseEvent e) {            }            @Override            public void mouseReleased(MouseEvent e) {                if (e.isPopupTrigger()) {                    JTree tree = (JTree) e.getComponent();                    TreePath path = tree.getPathForLocation(e.getX(), e.getY());                    tree.setSelectionPath(path);                    if (path == null) {                        System.out.println("Not click on the node...");                        return;                    }                    if (path.getParentPath() != null) {                        JMenuItem menu1 = new JMenuItem("menu1");                        JMenuItem menu2 = new JMenuItem("menu2");                        JMenuItem menu3 = new JMenuItem("menu3");                        JMenuItem menu4 = new JMenuItem("menu4");                        JMenuItem menu5 = new JMenuItem("menu5");                        JPopupMenu popupMenu1;                        popupMenu1 = new JPopupMenu();                        popupMenu1.add(menu3);                        popupMenu1.add(menu4);                        popupMenu1.add(menu5);                        popupMenu1.add(menu1);                        popupMenu1.add(menu2);                        menu1.addActionListener(new ActionListener() {                            public void actionPerformed(ActionEvent e) {                            }                        });                        popupMenu1.show(tree, e.getX(), e.getY());                    }                }            }        });
------解决方案--------------------------------------------------------
swing嘛,果断帮顶
  相关解决方案