I recently implemented Actionthat switches the activated status of a business function. Whenever a user invokes an action, I set a boolean flag: "willEnable" to determine whether the next call will enable or disable the function. Similarly, I am updating the action title, short description, and icon to reflect the new state. Then in my method, actionPerformed(...)I perform another state based action willEnable.
Action
actionPerformed(...)
willEnable
JToggleButton
JMenuBar
JButton
EDIT
In particular, how do they deal with applications such as IDEA? Will they use actions with several states (as indicated above) or exchange anotherAction with the specified one JButtonusing setAction(Action)? Perhaps this approach is better?
setAction(Action)
JPanel
ActionMap
Thanks in advance.
, GUI, "" ( Action ), . , erm, PropertyChangeEvent: , .
PropertyChangeEvent
, , . , . ToggleEnabledStatus, , . , , Action, willEnable, , erm, .
, ToggleButtonModel Action . , ,
ToggleButtonModel
.
, ( ) . , , , . ,
interface IState { void doAction(); boolean isEnabled(); } class EnabledState implement IState { void doAction() { setState(new DisabledState()); // do something } boolean isEnabled() {return true;} } class DisabledState implement IState { void doAction() { setState(new EnabledState()); // do nothing } boolean isEnabled() {return false;} } private IState state = new DisabledState(); // default is disabled private PropertyChangeSupport support = new PropertyChangeSupport(this); void setState(IState state) { if (this.state != state) { IState oldState = this.state; this.state = state; support.firePropertyChange("enabled", oldState.isEnabled(), state.isEnabled()); } } void doAction() { state.doAction(); }
, , , , , .
Source: https://habr.com/ru/post/1721781/More articles:How does the Java portlet work? - javaCompress xls content using apache deblate module - apache2размер файла резко увеличивается после рассола - pythonweb.config - automatically create release version - debuggingВ чем разница между jetspeed 2 и плутоном? - javaInstall BasicEditField Filter on BlackBerry - blackberryAccessing a dynamically loaded DLL (using LoadLibrary) in Visual Basic 6 - windowsЯзыковые строки в URL-адресах - urlC # how to handle internet breaks - c #How to work with memory leak of a third-party DLL (without source code) accessed by the Tomcat application? - javaAll Articles