使用JPopupMenu从JMenuItem获取JMenu

时间:2016-08-25 15:16:02

标签: java swing jmenu jmenuitem jpopupmenu

当我点击带有JPopupMenu的JMenuItem时,我想得到JMenu的名字。

我把ActionListener命名为" menuContextuelListener"在每个JMenuItem上:

ActionListener menuContextuelListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        System.out.println(event.getActionCommand());
    }
};

如何从JMenuItem中选择JMenu父级的名称?

我也在同一个监听器中尝试过这种方式,但它不起作用:

JMenuItem jmi = (JMenuItem) event.getSource();
JPopupMenu jpm = (JPopupMenu) jmi.getParent();
JMenu menu = (JMenu) jpm.getInvoker();

这个来自how to get the name of a JMenu when a JMenuItem is clicked

JPopupMenu menu = (JPopupMenu) ((JMenuItem) evt.getSource()).getParent();
JMenu actMenu = menu.getInvoker();

听众的完整代码:

ActionListener menuContextuelListener = new ActionListener(){
             public void actionPerformed(ActionEvent event)
              {

                     JMenuItem source = (JMenuItem)(event.getSource());
                        try{
                            JMenuItem menuItem = (JMenuItem) event.getSource(); 
                            JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent(); 
                            Component invoker = popupMenu.getInvoker();      
                            JPopupMenu popup = (JPopupMenu) invoker.getParent();
                            System.out.println("NAME OF JMENU: "+popup.getName());

                        }catch(Exception ex){
                            ex.printStackTrace();
                        }
              }
        };

这里是菜单结构的一个例子(动态):

tJMenu.add(new JMenu(ligne.substring(0, pos-1)));
tJMenu.get(tJMenu.size()-1).setName(ligne.substring(0, pos-1));

我查看下面的代码......我可以看到JMenu的名字:

System.out.println(tJMenu.get(tJMenu.size()-1).getName());

1 个答案:

答案 0 :(得分:0)

JMenuItem menuItem = (JMenuItem) event.getSource(); 
JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent(); 
Component invoker = popupMenu.getInvoker();      
//JPopupMenu popup = (JPopupMenu) invoker.getParent(); // why did you add this?

您在论坛中找到了有关如何执行此操作的建议,那么您为什么要更改找到的代码?

快速浏览一下,代码中有一个额外的getParent()语句。

调用者将成为JMenu。不需要第二个getParent()语句。

以下是一些更通用的代码,可让您获取菜单项和子菜单项的JMenu:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuItemAction implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        JMenuItem mi = (JMenuItem)e.getSource();
        mi.setText(mi.getText() + "0");
        JMenu menu = getMenuBarMenu(mi);
        System.out.println("Menu: " + menu.getText());

        JFrame frame = (JFrame)SwingUtilities.windowForComponent(menu);
        System.out.println("Frame: " + frame.getTitle());
    }

    private JMenu getMenuBarMenu(JMenuItem item)
    {
        JMenu menu = null;

        while (menu == null)
        {
            JPopupMenu popup = (JPopupMenu)item.getParent();
            item = (JMenuItem)popup.getInvoker();

            if (! (item.getParent() instanceof JPopupMenu) )
                menu = (JMenu)item;
        }

        return menu;
    }


    private static void createAndShowGUI()
    {
        ActionListener listener = new MenuItemAction();

        // Create menu bar

        JMenuBar menuBar = new JMenuBar();

        JMenu menu = new JMenu( "File" );
        menuBar.add( menu );

        JMenuItem item1 = new JMenuItem("Item 1");
        menu.add( item1 );

        JMenu subMenu1 = new JMenu("SubMenu 1");
        menu.add( subMenu1 );

        JMenuItem subItem1 = new JMenuItem("SubItem 1");
        subMenu1.add( subItem1 );

        JMenu subMenu12 = new JMenu("SubMenu 12");
        subMenu1.add( subMenu12 );

        JMenuItem subItem12 = new JMenuItem("SubItem 12");
        subMenu12.add( subItem12 );

        item1.addActionListener( listener );
        subItem1.addActionListener( listener );
        subItem12.addActionListener( listener );

        JFrame frame = new JFrame("Get Menu and Frame");
        frame.setJMenuBar( menuBar );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setSize(200, 200);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

将来当您提出问题时,请按照我的要求发布完整的SSCCE,以便我们确切了解您的工作。