How to prevent JMenuItem from closing Menu upon clicking the JMenuItem

折月煮酒 提交于 2019-12-06 00:56:37

First, using a menu to do this may be the wrong approach. JSpinner seems more appropriate. However, to do this with a JMenuItem you can subclass the MenuItemUI of the LookAndFeel you are using, and override the doClick(...) method so that clearSelectionPath() is not called, which closes the menu when the item is clicked.

Example, if you are using the Motif LookAndFeel you can do this:

menuItem.setUI(new MotifMenuItemUI() {
    @Override
    protected void doClick(MenuSelectionManager msm) {
        menuItem.doClick(0);
    }
});

I haven't ever tried this myself but I think it will work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!