I'm trying to make my JMenu disabled when all of its subitems are disabled. I have a menu "Add new" and in this menu two menu items: "File" and "Directory".
The menu items are bound to particular actions whose states I change, so menu items change their state as well.
What I'm trying to achieve is that the "Add new" menu gets disabled when both of "File" and "Directory" actions, thus items as well, are disabled.
I tried to override isSelected() method od JMenu and it partially works - it doesn't display the items. However, the menu is still painted as active (black font instead of gray).
Any thoughts on how to achieve this?
Here's a code sample that replicates the situation:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
JPopupMenu popup = new JPopupMenu();
final Action actionBeep = new DefaultEditorKit.BeepAction();
final Action actionPaste = new DefaultEditorKit.PasteAction();
JMenu menu = new JMenu("Add");
menu.add(new JMenuItem(actionBeep));
menu.add(new JMenuItem(actionPaste));
popup.add(menu);
JTable table = new JTable(3, 3);
table.setComponentPopupMenu(popup);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if(e.getClickCount() == 2) {
actionBeep.setEnabled(!actionBeep.isEnabled());
actionPaste.setEnabled(!actionPaste.isEnabled());
}
}
});
frame.add(table);
frame.pack();
frame.setVisible(true);
}
});
}