05-Events - Event Handling (Part 2) 1 Focus Events Window...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1 Event Handling (Part 2) 2 Focus Events ± Window manager directs all keystrokes to the active window ± A component has focus if it can receive keyboard events • Window: highlighted title bar • Text field: blinking cursor • Button: rectangle around label ± Only one component at a time can have the focus • A component gains focus if user clicks inside the component • A component loses focus if user clicks on another component ± Use the TAB key to give focus to each component in turn • Swing components are traversed from left to right, then top to bottom
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 Focus Events ± Focus owner • The component that has focus • Use getFocusOwner of KeyboardFocusManager class ± Focused window • The window that contains the focus owner • Use getFocusedWindow of KeyboardFocusManager class KeyboardFocusManager manager = KeyboardFocusManager. getCurrentKeyboardFocusManager(); Component owner = manager.getFocusOwner(); Window focused = manager.getFocusedWindow(); 4 Focus Events ± Methods of the Component class void requestFocus() void setFocusable(boolean b) boolean isFocusable() boolean isFocusOwner()
Background image of page 2
3 5 Focus Events ± Methods of FocusListener interface focusGained focusLost ± Methods of WindowFocusListener interface windowGainedFocus windowLostFocus 6 Focus Events ± Methods of FocusEvent getComponent isTemporary ± The getComponent method reports the component that gained or lost the focus ± A temporary focus change occurs when a component loses control temporarily but will automatically get it back • For instance, when the user selects a different window
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 Focus Events Example ± When the user is done editing the text field (with a credit card number) and moves to another field, trap the lost focus event ± If the credit card number was not formatted properly, give the focus back to the text field public void focusLost(FocusEvent event) { if (event.getComponent() == ccField && !event.isTemporary()){ if (!checkFormat(ccField.getText()) ccField.requestFocus(); } } 8 Focus Events ± When a component or window has lost focus, the opposite is the component or window that gains focus •U se the getOppositeComponent method of FocusEvent class to report the opposite component •U se the getOppositeWindow
Background image of page 4
5 9 Keyboard Events ± KeyEvent • Key pressed and key released events, reporting the actual key public static final int KEY_PRESSED public static final int KEY_RELEASED • Key typed event, reporting the character generated by the keystroke public static final int KEY_TYPED 10 Keyboard Events ± Methods of KeyListener interface keyPressed (KeyEvent e) keyReleased (KeyEvent e) keyTyped (KeyEvent e)
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 11 Keyboard Events ± Virtual key codes correspond to keys on the keyboard • Indicated with a prefix of VK_ , such as VK_A and VK_SHIFT • There is no separate lowercase or uppercase virtual key codes 12 Keyboard Events ± Typing an uppercase “A” involves 5 key events
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

05-Events - Event Handling (Part 2) 1 Focus Events Window...

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

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