How to prevent a disabled JMenuItem from hiding the menu when being clicked?

Brad :

In my Java swing application i have noticed that when i click on a disabled JMenuItem in a JPopupMenu it hides the menu, but i i do not want to hide it, as if nothing is clicked. Is there a way to prevent this ?

-----------------------------------> Update: Added Code sample :

JMenuItem saveMenuItem = new JMenuItem();

saveMenuItem.setEnabled(false);

saveMenuItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        saveMenuItemActionPerformed();
    }
});
add(saveMenuItem);

private void saveMenuItemActionPerformed() {
    System.out.println( "Save clicked." );
}
Alba Mendez :

This has been tested and works.

The Look & Feel decides how to handle the mouse events on disabled menu items. Anyway, you can intercept the undesired events by using a custom MenuItem. Simply use that code (copy/paste):

public class CustomMenuItem extends JMenuItem {

    public CustomMenuItem(String text) {
        super(text);
    }

    public CustomMenuItem() {
        super();
    }

    protected void processMouseEvent(MouseEvent e) {
        if (isEnabled()) super.processMouseEvent(e);
    }
}

First, adapt the code to suit your needs (optional).
Finally, replace any JMenuItem with a CustomMenuItem.

That's it!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to prevent custom DialogFragment from hiding keyboard when being shown

How to prevent an input from being disabled?

How can I prevent a child from being clicked when the parent is being dragged?

How to prevent a row count from being sent to the db when paginator is disabled

How do I change a hamburger menu svg from animating when it is being hovered on to animating when it is clicked

How can I prevent images from being zoomed in/out when clicked?

Menu hiding when outside 'div' element is clicked

When creating a WPF application, how do I prevent a menu bar from being focused with tab

How to prevent button from being clicked more than 1 in a lifetime

How to prevent bottom navigation from hiding when scrolling a CoordinatorLayout

How to show/hide a section of content when a corresponding menu item is clicked, while hiding all other content?

Hiding submenuitems of jQuery UI Menu when parent is disabled

Prevent button from being clicked programmatically

Prevent menu from rolling up when its sub-menu is clicked

How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

How to prevent HTML button from moving when clicked

MUI: Do not close Menu when clicked on disabled MenuItem

How to prevent a website from hiding its scrollbar?

How to prevent spans from hiding bar charts?

How to prevent Highstock from hiding data rows

Need to stop propagation in order to prevent menu from closing when clicked inside dropdown content

How to prevent XSS vulnerability from being introduced when using .val()?

How to prevent file from being overridden when reading and processing it with Java?

How to prevent $() from being executed prematurely when creating an alias

How to prevent a thread from being terminated when conditions are not satisfied momentarily?

How to prevent score from being overwritten when the same activity is called

How to prevent UIPopoverPresentationController from being dismissed when clicking outside popover?

How to prevent data from being lost when page refreshed?

How to prevent a Fragment from being added when an Activity is closing