I have written a Swing GUI with several controls associated with the same Action
subclass. The implementation of the Action
subclass follows this psudocode:
public class MyGUI
{
Gizmo gizmo_; // Defined elsewhere
public class Action_StartPlayback extends AbstractAction
{
/* ctor */
public Action_StartPlayback(String text, ImageIcon icon, String desc, Integer mnem)
{
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnem);
}
@Override public boolean isEnabled()
{
return gizmo_ == null;
}
@Override public void actionPerformed(ActionEvent e)
{
gizmo_ = new Gizmo();
}
Action_StartPlayback act_;
};
The action is associated with both a button and a menu item, in a way similar to this psudocode:
act_ = new Action_StartPlayback(/*...*/);
// ...
JButton btn = new JButton(act_);
JMenu mnu = new JMenu(act_);
When I click the button or the menu item, the action's actionPerformed
is fired correctly, gizmo_
is initialized and is non-null
and everything works as expected -- except that the button and menu item are still enabled.
I expected that isEnabled
would have been called again "automagically" but this is obviously not happening. isEnabled()
is never called again.
This evokes two questions:
- Is it OK for me to
@Override
theisEnabled()
method as I have done here? - Assuming the answer to #1 is yes, how do I trigger a refresh of the GUI so that
isEnabled()
is called again, resulting in the button & menu item being disabled?