06-Events - Event Handling(Part 3 1 Event Handling One...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1 Event Handling (Part 3) 2 Event Handling One event source and one event listener Button ColorAction Action Event
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 3 Event Handling Multiple ways to activate the same command – link all the event sources to the same listener Menu item (“Blue”) Button (“Blue”) Keystroke (“CTRL+B”) Menu Item ColorAction Action Event Button Keystroke Action Event Action Event 4 Command Abstraction Swing uses the Action interface to encapsulate commands and to attach them to multiple event sources An action is an object that encapsulates A description of the command (as a text string and an optional icon) Parameters that are necessary to carry out the command (such as the requested color in the example) It enables information concerning the command to be located in one place It also enables the command to be enabled or disabled in one place
Image of page 2
3 5 The Action Interface The Action interface extends the ActionListener interface void actionPerformed(ActionEvent event) void setEnabled(boolean b) boolean isEnabled() void putValue(String key, Object value) Object getValue(String key) void addPropertyChangeListener (PropertyChangeListener listener) void removePropertyChangeListener (PropertyChangeListener listener) Implemented by AbstractAction class 6 Action Interface Methods setEnabled() and isEnabled() Enable or disable the action and check whether the action is currently enabled If an action is attached to a menu or toolbar, the option is grayed out when the action is disabled
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon