Question

I hava a JPopupMenu and am inserting some JMenuItems in it. What I need is a JMenuItem that looks same as normal doe but is not clickable.

I tried it with:

JPopupMenu popmen = new JPopupMenu();

JMenuItem menu1 = new JMenuItem("Add new Datasource:");
JMenuItem menu2 = new JMenuItem("Join");

menu1.setEnabled(false);
popmen.add(menu1);
popmen.addSeparator();
popmen.add(menu2);

The problem here is that the appearance of menu1 is changing by using setEnabled method. Is there any method to do it or do I have to change the appearance of the not enabled JMenuItem back to a normal JMenuItem?

I need a JMenuItem that doesnt do anything. No Mouseover events not clickable just a header category for the other JMenuItems. For example a empty JMenuItem has still mouseover events. I'm searching for a method or property that eliminates all the effects.

So do I have to rewrite all the events to do this?

Was it helpful?

Solution

Since the purpose is to create a header, rather than just an inactive component, you can use a JLabel for the purpose. In general you can add any JComponent to a menu.

To avoid confusing users it should be clearly differentiated from normal menu items. Most software does not have headers in menus, so you should consider if adding them will cause more problems than not having them.

It is possible to take advantage of familiar user interface elements from elsewhere, which I use to justify a web style megamenu below (could not bring myself to introduce headers to a normal menu, so excuse the complication of using custom layout, and directly adding to the popup because of that):

enter image description here

The code for that:

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;

public class MenuSample {
    MenuSample() {
        JFrame frame = new JFrame("Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu menu = new JMenu("Megamenu");
        JPopupMenu popup = menu.getPopupMenu(); 
        popup.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        menuBar.add(menu);
        gbc.gridx = 0;
        gbc.gridy = 0;
        popup.add(createHeader("Header 1"), gbc);
        gbc.gridy++;
        popup.add(new JMenuItem("Item 1"), gbc);
        gbc.gridy++;
        popup.add(new JMenuItem("Item 2"), gbc);
        gbc.gridy++;
        popup.add(new JMenuItem("Item 3"), gbc);
        gbc.gridx++;
        gbc.gridy = 0;

        popup.add(createHeader("Header 2"), gbc);
        gbc.gridy++;
        popup.add(new JMenuItem("Item 3"), gbc);
        gbc.gridy++;
        popup.add(new JMenuItem("Item 4"), gbc);
        gbc.gridy++;
        popup.add(new JMenuItem("Item 5"), gbc);

        frame.pack();
        frame.setVisible(true);
    }

    private JComponent createHeader(String header) {
        JLabel label = new JLabel(header);
        label.setFont(label.getFont().deriveFont(Font.ITALIC));
        label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        return label;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MenuSample();
            }
        });
    }
}

OTHER TIPS

You can override (and redefine) the clickable method. You can have a custom JMenuItem.

If you do not define any actions to trigger when the menu is clicked then it would effectively make the menu unclickable. If you want to define a menu item component that is unclickable then you may also extend the JMenuItem class and override the addActionListener or addItemListener methods to reject the listener so that no action would be triggered when the menu is clicked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top