Enable/disable action buttons
According to this post, action buttons can be enabled or disabled by the action itself, in the actionPerformed() method.
Now, suppose I disable a button this way. So, I could only re-enable it the same way, i.e., when the action is performed. But if the button is disabled, the action will never be performed again. It seems we have a deadlock: to enable the button, I need to perform the action, but to perform the action, I need to enable the button.
What am I missing here?
Thank you.
Otávio Macedo.
Please sign in to leave a comment.
Oh, nevermind. I have already found the answer for my own question.
The right place to enable/disable the button is in the update() method.
Otavio Macedo.
I need to disable my "Debug" button (created as "AnAction") programmatically when the debug process starts.
How can I create an instance of AnActionContext without providing whole bunch of parameters in its constructor? Is there an easier way?
I could add my own parameter to the Action constructor and check it in "update" method (completely ignoring "AnActionEvent" parameter)...
Hello Alexey,
Why would you need to create an instance of AnActionEvent in order to disable
a button? IntelliJ IDEA will call the update() method of your AnAction with
its own AnActionEvent every 500 ms.
Indeed, you can do so. If your action has a local context, this is a perfectly
valid thing to do.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Thanks, Dmitry. Here's the problem: I see update() method is only invoked on Actions when I move cursor over the window or press any key.
I start Gosu debug process using my "Debug" button (see the screenshot), then wait for ~1 second until it's completed.
I see my "Debug" button *still disabled* and "stop debug" is still enables because their update() methods won't be called until I move the mouse over that "Gosu Tester" window.
That's why I need a way to force status change on the Action when I know that the debug process is completed.
Attachment(s):
button_state_unchanged.png
Hello Alexey,
Where is your action located? Actions on ActionToobars are updated by IDEA
by timer, independently of keypresses.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
They are located on the left part of tool window (see the attached screenshot above)