Java Swing - Add leniency when selecting items in submenus

Call setDelay(delay) on your menu variable, where the delay parameter is the amount of milliseconds to wait for the menu to show, as an int.

This following line of code will set the delay to 1 second, so the user has to mouseover the menu item "Menu n" 1 second, before the submenu is displayed: menu.setDelay(1000);

Here's a snippet of the edited code:

for (int i = 0; i < 5; i++)
{
    JMenu menu = new JMenu("Menu " + i);
    pMenu.add(menu);
    for (int j = 0; j < 10; j++)
    {
        menu.add(new JMenuItem("Item " + j));
    }
    menu.setDelay(1000);
}