Getting the JMenu object from the selected JMenuItem

Get the accessible parent from the accessibleContext.
Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame implements ActionListener {
   private JMenu menu1;
   private JMenu menu2;
 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JMenuBar menuBar = new JMenuBar();
      menu1 = new JMenu("The weak sex");
      JMenuItem menuItem1 = new JMenuItem("male");
      menu2 = new JMenu("The strong sex");
      JMenuItem menuItem2 = new JMenuItem("female");
      menu1.add(menuItem1);
      menu2.add(menuItem2);
      menuBar.add(menu1);
      menuBar.add(menu2);
      menuItem1.addActionListener(this);
      menuItem2.addActionListener(this);
 
      this.setJMenuBar(menuBar); 
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public void actionPerformed(ActionEvent ae) {
      JMenuItem menuItem = (JMenuItem) ae.getSource();
      JMenu menu = (JMenu) menuItem.getAccessibleContext().getAccessibleParent();
      System.out.println("Selected menu: " + menu.getText());
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}