简体   繁体   中英

How to trigger an action when clicking a disabled JMenuItem?

I did a bit of googling and poking around in SO but all examples are find are for cases when the JMenuItem is enabled.

Context for what I'm trying to do is that I want my disabled JMenuItem (because of limited privileges), when clicked, to display a pop up box requesting that the user upgrade so that they can access said JMenuItem .

The following is a stripped down version of what I currently have, nothing got printed out on the command line:

public class ExportMenuItem extends JMenuItem
{
    public ExportMenuItem()
    {
        super("Menu Item Name");

        addMouseListener(new MouseAdapter()
            {
                @Override
                public void mouseClicked(MouseEvent mouseEvent)
                {
                    if (!isEnabled())
                    {
                        JOptionPane.showMessageDialog(editor.getFrame(), "Hello world.");
                        System.out.println("Sys print hello.");
                    }
                    System.out.println("Sys print hello outside.");
                }
            });
    }
}

is this what you are looking for?

import javax.swing.*;
import java.awt.event.*;

public class ExportMenuItem extends JMenuItem{

    public ExportMenuItem(){
        super("menu item");

        addMouseListener(new MouseListener(){
                @Override
                public void mouseClicked(MouseEvent mouseEvent){
                    if (!isEnabled())                    {
                        JOptionPane.showMessageDialog(null, "Upgrade me!");
                    }//end of if
                }//end of mouseClicked
                public void mouseExited(MouseEvent mouseEvent){}
                public void mouseEntered(MouseEvent mouseEvent){}
                public void mouseReleased(MouseEvent mouseEvent){}
                public void mousePressed(MouseEvent mouseEvent){}

                // And the remaining methods to implement...
            });//end of anonymous class
    }//end of constructor

    public static void main(String[] a){
        JFrame f = new JFrame();
        JMenu menu = new JMenu("menu");
        JMenuBar menuBar  = new JMenuBar();
        f.setJMenuBar(menuBar);
        f.setSize(300, 300);
        f.setVisible(true);
        menuBar.add(menu);
        JMenuItem item = new ExportMenuItem();
        menu.add(item);
        item.setEnabled(false);
    }//end of main
}//end of class

Maybe a complete different approach, that is more logical for users:

Place a describing text behind the menu item:

private void addRestartHint(JMenuItem m, String text) {
    final String spaceholder = "                                        ";
    String t = m.getText() + spaceholder;
    m.setLayout(new BorderLayout());
    m.setText(t);
    m.add(new JLabel(text), BorderLayout.EAST);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM