2

I would like to get the name of the JMenu when I click on my JMenuItem with a JPopupMenu.

I put an ActionListener named "menuContextuelListener" on each JMenuItem:

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

How can I get the name of the JMenu parent from JMenuItem selected?

I also tried this way in the same listener, but it doesn't work :

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

And this one from how to get the name of a JMenu when a JMenuItem is clicked:

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

Full code of the listener :

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

Here an example of the construction of the menu (dynamically) :

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

and I check with the code below... and I can see the names of the JMenu :

System.out.println(tJMenu.get(tJMenu.size()-1).getName());
Community
  • 1
  • 1
Fred
  • 169
  • 1
  • 3
  • 19

1 Answers1

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

You found suggestions in the forum on how to do this so why did you change the code you found?

From a quick glance, you have an extra getParent() statement in your code.

The invoker will be the JMenu. There is no need for the second getParent() statement.

Here is some more generic code that will allow you to get the JMenu for menu items and sub menu items:

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();
            }
        });
*/
    }
}

In the future when you ask a question, please post a complete SSCCE as I have done so we can see exactly what you are doing.

camickr
  • 321,443
  • 19
  • 166
  • 288