I have recently implemented an Action
that toggles the enabled status of a business function. Whenever the user invokes the action I set a boolean flag: "willEnable" to determine whether the next invocation will enable or disable the function. Likewise I update the action name, short description and icon to reflect the new state. Then within my actionPerformed(...)
method I take a different action based on the state of willEnable
.
- Is this the correct approach or can
anyone recommend a better one (as I
suspect this is a common problem)?
(I can see that
JToggleButton
acts as a two-state button but I want this Action to be visible on aJMenuBar
as well as aJButton
, so do not think this is appropriate).
EDIT
Specifically, how do applications like IDEA deal with this? Would they use multi-state actions (as above) or would they swap a different Action
into a given JButton
using setAction(Action)
? Perhaps this approach is better?
- When updating an action's properties
can I rely on GUI components
initialised with that
Action
(e.g.JButton
) automatically repainting themselves? What if theJButton
size changes as a result? Should I be revalidating the containingJPanel
myself? - Is changing the action's name a bad
thing to do? This is the only way I
can make the JButton text change, but am concious that the name should probably remain constant if the action is being placed in an
ActionMap
.
Thanks in advance.