Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : F words : focus.
Component.requestFocus won't work until after the frame is completely painted. This means you must usually put such code inside a WindowListener.
Component.transferFocus lets you hand focus off to some other component. Component.hasFocus lets you determine if a given Component has focus. KeyboardFocusManagerI.getFocusOwner lets you ask which Component currently has focus.
Call getFocusOwner on all windows in your application. (Frame.getFrames() followed by calling getOwnedWindows on each of them recursively should get you a full list).
JDK 1.4 now handles focus and traversal with in totally new way with FocusTraversalPolicy. JSpinners will drive you little crazy in FocusTraversalPolicy.getComponentAfter, because you don't get passed the address of the JSpinner, but rather an associated JFormattedTextField that helps implement it. I consider this a bug. You have to use Jspinner.getEditor.getComponent(0) in FocusTraversalPolicy.getComponentAfter method.
Prior to 1.4 there were over a hundred outstanding bugs in focus handling. You had to use isManagingFocus(false).
Handling focus is still quite complicated. This overall specification might help.
/** * tab keystroke to be used for forward traversal. */ private static Set tab = new HashSet (1); static { tab.add(KeyStroke.getKeyStroke("TAB" )); } /** * shift-tab keystroke to be used for backward traversal. */ private static Set shifttab = new HashSet (1); static { shifttab.add(KeyStroke.getKeyStroke( "shift TAB" )); } /** * Fix component so Tab and Shift-Tab work to * traverse as well as Ctrl-Tab and Ctrl-Shift-Tab. * Only works in JDK 1.4+ * * @param c component to fix, usually a JTextArea */ public static void fixTab ( Component c ) { c.setFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, tab ); c.setFocusTraversalKeys( KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, shifttab ); }
theButton.setFocusPainted( false );
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 1476. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/focus.html | J:\mindprod\jgloss\focus.html | |||