日期:2014-05-20  浏览次数:20900 次

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嘛,果断帮顶