package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public final class MainPanel extends JPanel {
    private MainPanel() {
        super(new BorderLayout());

        JCheckBox check = new JCheckBox("JMenu: hover(show popup automatically) on cursor", true);
        JMenuBar bar = makeMenuBar();
        visitAll(bar, new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e) {
                if (check.isSelected()) {
                    ((AbstractButton) e.getComponent()).doClick();
                }
            }
            @Override public void mouseEntered(MouseEvent e) {
                if (check.isSelected()) {
                    ((AbstractButton) e.getComponent()).doClick();
                }
            }
        });

        EventQueue.invokeLater(() -> {
            Component c = SwingUtilities.getRoot(this);
            if (c instanceof JFrame) {
                ((JFrame) c).setJMenuBar(bar);
            }
        });

        add(check, BorderLayout.SOUTH);
        setPreferredSize(new Dimension(320, 240));
    }
    private static JMenuBar makeMenuBar() {
        JMenuBar bar = new JMenuBar();

        JMenu menu = new JMenu("File");
        menu.add(makeMenuItem("Open"));
        menu.add(makeMenuItem("Save"));
        menu.add(makeMenuItem("Exit"));
        bar.add(menu);

        menu = new JMenu("Edit");
        menu.add(makeMenuItem("Undo"));
        menu.add(makeMenuItem("Redo"));
        menu.addSeparator();
        menu.add(makeMenuItem("Cut"));
        menu.add(makeMenuItem("Copy"));
        menu.add(makeMenuItem("Paste"));
        menu.add(makeMenuItem("Delete"));
        bar.add(menu);

        menu = new JMenu("Test");
        menu.add(makeMenuItem("JMenuItem1"));
        menu.add(makeMenuItem("JMenuItem2"));
        JMenu sub = new JMenu("JMenu");
        sub.add(makeMenuItem("JMenuItem4"));
        sub.add(makeMenuItem("JMenuItem5"));
        menu.add(sub);
        menu.add(makeMenuItem("JMenuItem3"));
        bar.add(menu);
        return bar;
    }
    private static void visitAll(Container p, MouseListener l) {
        for (Component comp: p.getComponents()) {
            if (comp instanceof JMenu) {
                ((JMenu) comp).addMouseListener(l);
            }
//             if (comp instanceof Container) {
//                 Container c = (Container) comp;
//                 if (c.getComponentCount() > 0) {
//                     visitAll(c, l);
//                 }
//                 if (c instanceof JMenu) {
//                     c.addMouseListener(l);
//                 }
//             }
        }
    }
    private static JMenuItem makeMenuItem(String str) {
        JMenuItem item = new JMenuItem(str);
        item.addActionListener(e -> Toolkit.getDefaultToolkit().beep());
        return item;
    }
    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

@@ -1316,29 +1316,15 @@
 add(
-makeMenuItem(
 %22Open%22)
-)
 ;%0A  
@@ -1342,29 +1342,15 @@
 add(
-makeMenuItem(
 %22Save%22)
-)
 ;%0A  
@@ -1368,29 +1368,15 @@
 add(
-makeMenuItem(
 %22Exit%22)
-)
 ;%0A  
@@ -1452,29 +1452,15 @@
 add(
-makeMenuItem(
 %22Undo%22)
-)
 ;%0A  
@@ -1478,29 +1478,15 @@
 add(
-makeMenuItem(
 %22Redo%22)
-)
 ;%0A  
@@ -1533,28 +1533,14 @@
 add(
-makeMenuItem(
 %22Cut%22)
-)
 ;%0A  
@@ -1558,29 +1558,15 @@
 add(
-makeMenuItem(
 %22Copy%22)
-)
 ;%0A  
@@ -1584,29 +1584,16 @@
 add(
-makeMenuItem(
 %22Paste%22)
 );%0A 
@@ -1584,25 +1584,24 @@
 add(%22Paste%22)
-)
 ;%0A        me
@@ -1607,29 +1607,16 @@
 enu.add(
-makeMenuItem(
 %22Delete%22
@@ -1616,17 +1616,16 @@
 Delete%22)
-)
 ;%0A      
@@ -1689,37 +1689,24 @@
    menu.add(
-makeMenuItem(
 %22JMenuItem1%22
@@ -1706,17 +1706,16 @@
 uItem1%22)
-)
 ;%0A      
@@ -1721,37 +1721,24 @@
    menu.add(
-makeMenuItem(
 %22JMenuItem2%22
@@ -1738,17 +1738,16 @@
 uItem2%22)
-)
 ;%0A      
@@ -1792,37 +1792,24 @@
     sub.add(
-makeMenuItem(
 %22JMenuItem4%22
@@ -1809,17 +1809,16 @@
 uItem4%22)
-)
 ;%0A      
@@ -1823,37 +1823,24 @@
     sub.add(
-makeMenuItem(
 %22JMenuItem5%22
@@ -1840,17 +1840,16 @@
 uItem5%22)
-)
 ;%0A      
@@ -1882,29 +1882,16 @@
 enu.add(
-makeMenuItem(
 %22JMenuIt
@@ -1895,17 +1895,16 @@
 uItem3%22)
-)
 ;%0A      
@@ -2528,209 +2528,8 @@
   %7D%0A
-    private static JMenuItem makeMenuItem(String str) %7B%0A        JMenuItem item = new JMenuItem(str);%0A        item.addActionListener(e -%3E Toolkit.getDefaultToolkit().beep());%0A        return item;%0A    %7D%0A
     

use JMenu#add(String) instead of JMenu#add(new JMenuItem(String))