Separating GUI and Application Code
In large applications, some Java experts recommend that it is a better design to separate the responsibilities of getting user input and executing commands because it is common to have multiple ways to activate a command.
- Make an object for every command
- Each command object is a listener for the events that trigger it
The Swing package provides the Action interface to encapsulate commands and attach them to multiple event sources. The Action interface implements the ActionListener interface and has additional methods for properties of the command:
- void actionPerformed ( ActionEvent e)
- void setEnabled (boolean b), boolean isEnabled ( )
- void putValue (String key, Object val), Object getValue ( String key )
- addPropertyChangeListener, removePropertyChangeListener