Reputation: 1062
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.");
}
});
}
}
Upvotes: 1
Views: 304
Reputation: 1210
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);
}
Upvotes: 0
Reputation: 21
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
Upvotes: 2