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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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”) •Key s t roke ( “CTR L+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)
Background 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 7 Action Interface Methods ± putValue and getValue methods store and retrieve arbitrary name/value pairs in the action object ± There are a few predefined strings Action.NAME Action.SMALL_ICON (for toolbars and menus) Action.SHORT_DESCRIPTION (as tool tips) Action.MNEMONIC_KEY Action.ACCELERATOR_KEY putValue(Action.NAME, “Blue”) putValue(Action.SMALL_ICON, new ImageIcon(“blue_ball.gif”)); 8 Action Interface Methods ± addPropertyChangeListener and removePropertyChangeListener • Allow other objects, in particular menus or toolbars that trigger the action, to be notified when the properties of the action object change • For example, if a menu is added as a property change listener of an action object and the action object is subsequently disabled, then the menu is called and can gray out the action name
Background image of page 4
5 9 AbstractAction Class ± Action is an interface, not a class • You have to implement all seven methods ± The AbstractAction class implements all methods except for actionPerformed • It takes care of storing all name/value pairs and managing the property change listeners • We simply extend it and supply an actionPerformed method 10 AbstractAction Class public class ColorAction extends AbstractAction {
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

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

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online