0

In my application, I have a JPopupMenu that displays a set of sub-menus:

private static JMenu createMenu(String title) {
    JMenu menu = new JMenu(title);
    menu.setDelay(2000);
    menu.add(new JMenuItem("123"));
    menu.add(new JMenuItem("234"));
    menu.add(new JMenuItem("345"));
    return menu;
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Hello");
    final JButton button = new JButton("Test");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JPopupMenu jpm = new JPopupMenu();
            jpm.add(createMenu("XXX"));
            jpm.add(createMenu("YYY"));
            jpm.add(createMenu("ZZZ"));
            jpm.show(button, 0, 0);
        } 
    });

    frame.add(button);

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
}

This application usually runs on Windows.

I first mouse-over the first XXX sub-menu. Sometimes, I accidentally move my mouse over YYY, which then causes the first sub-menu to disappear immediately.

From reading the Javadoc, it would seem that calling JMenu.setDelay(2000) should suggest that the JMenu's popup menu wait 2 seconds before popping down. However, it only seems to delay the next menu 2 seconds before popping up.

Is there a way to delay the pop down?

Huey
  • 2,714
  • 6
  • 28
  • 34

1 Answers1

1

Update

Here's something interesting; from Bug #6563939 Delay should be respected before hiding a JMenu, open since 2007

Javadoc might be misleading in this point. It says "before submenus are popped up or down". "Popping down" could be understood as "hiding", but it rather has the meaning of "submenu is rendered below his parent menu".


It also seems to suggest that this depends on the L&F you're using, so it looks like more like something you can only request or hint at:

Each look and feel (L&F) may determine it's own policy for observing the delay property. In most cases, the delay is not observed for top level menus or while dragging. This method is a property of the look and feel code and is used to manage the idiosyncracies of the various UI implementations.

no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • @Huey I've updated my answer with a link to a bug that seems to point out this very issue, open since 2007 – no.good.at.coding Mar 09 '11 at 03:06
  • I suppose if you really wanted the delay, you could extend `JMenu` and do something tricky there - although I took a look at the source for `JMenu` and there doesn't seem to be a straight-forward way to do it – no.good.at.coding Mar 09 '11 at 03:44