src/java.desktop/share/classes/java/awt/doc-files/FocusSpec.html
changeset 50358 1ba28f0dbc33
parent 48264 efda6932a433
child 58595 fa9f28a0e175
equal deleted inserted replaced
50357:2e0c4b2d567b 50358:1ba28f0dbc33
     3 <head>
     3 <head>
     4   <meta charset="utf-8"/>
     4   <meta charset="utf-8"/>
     5   <title>The AWT Focus Subsystem</title>
     5   <title>The AWT Focus Subsystem</title>
     6 </head>
     6 </head>
     7 <!--
     7 <!--
     8  Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
     8  Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
     9  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     9  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    10 
    10 
    11  This code is free software; you can redistribute it and/or modify it
    11  This code is free software; you can redistribute it and/or modify it
    12  under the terms of the GNU General Public License version 2 only, as
    12  under the terms of the GNU General Public License version 2 only, as
    13  published by the Free Software Foundation.  Oracle designates this
    13  published by the Free Software Foundation.  Oracle designates this
    28  or visit www.oracle.com if you need additional information or have any
    28  or visit www.oracle.com if you need additional information or have any
    29  questions.
    29  questions.
    30 -->
    30 -->
    31 
    31 
    32     <body>
    32     <body>
       
    33     <main role="main">
    33       <h1>The AWT Focus Subsystem</h1>
    34       <h1>The AWT Focus Subsystem</h1>
    34 
    35 
    35     <p>
    36     <p>
    36       Prior to Java 2 Standard Edition, JDK 1.4, the AWT focus subsystem
    37       Prior to Java 2 Standard Edition, JDK 1.4, the AWT focus subsystem
    37       was inadequate. It suffered from major design and API problems,
    38       was inadequate. It suffered from major design and API problems,
    99       <li><a href=#ReplacingDefaultKeyboardFocusManager>Replacing DefaultKeyboardFocusManager</a>
   100       <li><a href=#ReplacingDefaultKeyboardFocusManager>Replacing DefaultKeyboardFocusManager</a>
   100       <li><a href=#Incompatibilities>Incompatibilities with Previous Releases</a>
   101       <li><a href=#Incompatibilities>Incompatibilities with Previous Releases</a>
   101      </ul>
   102      </ul>
   102 
   103 
   103       <a id="Overview"></a>
   104       <a id="Overview"></a>
   104       <h3>Overview of KeyboardFocusManager</h3>
   105       <h2>Overview of KeyboardFocusManager</h2>
   105     <p>
   106     <p>
   106       The focus model is centralized around a single class,
   107       The focus model is centralized around a single class,
   107       KeyboardFocusManager, that provides a set of APIs for client code to
   108       KeyboardFocusManager, that provides a set of APIs for client code to
   108       inquire about the current focus state, initiate focus changes, and
   109       inquire about the current focus state, initiate focus changes, and
   109       replace default focus event dispatching with a custom dispatcher.
   110       replace default focus event dispatching with a custom dispatcher.
   211 <code>KeyboardFocusManager</code> is an abstract class. AWT provides a default
   212 <code>KeyboardFocusManager</code> is an abstract class. AWT provides a default
   212 implementation in the <code>DefaultKeyboardFocusManager</code> class.
   213 implementation in the <code>DefaultKeyboardFocusManager</code> class.
   213 
   214 
   214 
   215 
   215 <a id="BrowserContexts"></a>
   216 <a id="BrowserContexts"></a>
   216 <h3>KeyboardFocusManager and Browser Contexts</h3>
   217 <h2>KeyboardFocusManager and Browser Contexts</h2>
   217 <p>
   218 <p>
   218 Some browsers partition applets in different code bases into separate
   219 Some browsers partition applets in different code bases into separate
   219 contexts, and establish walls between these contexts. Each thread and
   220 contexts, and establish walls between these contexts. Each thread and
   220 each Component is associated with a particular context and cannot
   221 each Component is associated with a particular context and cannot
   221 interfere with threads or access Components in other contexts. In such
   222 interfere with threads or access Components in other contexts. In such
   227 contexts there may be, however, there can never be more than one focus
   228 contexts there may be, however, there can never be more than one focus
   228 owner, focused Window, or active Window, per ClassLoader.
   229 owner, focused Window, or active Window, per ClassLoader.
   229 
   230 
   230 
   231 
   231 <a id="KeyEventDispatcher"></a>
   232 <a id="KeyEventDispatcher"></a>
   232 <h3>KeyEventDispatcher and KeyEventPostProcessor</h3>
   233 <h2>KeyEventDispatcher and KeyEventPostProcessor</h2>
   233 <p>
   234 <p>
   234 While the user's KeyEvents should generally be delivered to the focus
   235 While the user's KeyEvents should generally be delivered to the focus
   235 owner, there are rare cases where this is not desirable. An input
   236 owner, there are rare cases where this is not desirable. An input
   236 method is an example of a specialized Component that should receive
   237 method is an example of a specialized Component that should receive
   237 KeyEvents even though its associated text Component is and should
   238 KeyEvents even though its associated text Component is and should
   270 Like KeyEventDispatcher, KeyboardFocusManager also implements
   271 Like KeyEventDispatcher, KeyboardFocusManager also implements
   271 KeyEventPostProcessor, and similar restrictions apply to its use in
   272 KeyEventPostProcessor, and similar restrictions apply to its use in
   272 that capacity.
   273 that capacity.
   273 
   274 
   274 <a id="FocusEventAndWindowEvent"></a>
   275 <a id="FocusEventAndWindowEvent"></a>
   275 <h3>FocusEvent and WindowEvent</h3>
   276 <h2>FocusEvent and WindowEvent</h2>
   276 <p>
   277 <p>
   277 The AWT defines the following six event types central to the focus
   278 The AWT defines the following six event types central to the focus
   278 model in two different <code>java.awt.event</code> classes:
   279 model in two different <code>java.awt.event</code> classes:
   279   <ol>
   280   <ol>
   280     <li><code>WindowEvent.WINDOW_ACTIVATED</code>: This event is
   281     <li><code>WindowEvent.WINDOW_ACTIVATED</code>: This event is
   294         dispatched to a Frame or Dialog (but never a Window which is
   295         dispatched to a Frame or Dialog (but never a Window which is
   295         not a Frame or Dialog) when it is no longer the active Window.
   296         not a Frame or Dialog) when it is no longer the active Window.
   296   </ol>
   297   </ol>
   297 
   298 
   298 <a id="EventDelivery"></a>
   299 <a id="EventDelivery"></a>
   299 <h3>Event Delivery</h3>
   300 <h2>Event Delivery</h2>
   300 <p>
   301 <p>
   301 If the focus is not in java application and the user clicks on a focusable
   302 If the focus is not in java application and the user clicks on a focusable
   302 child Component<b>a</b> of an inactive Frame <b>b</b>, the following events
   303 child Component<b>a</b> of an inactive Frame <b>b</b>, the following events
   303 will be dispatched and handled in order:
   304 will be dispatched and handled in order:
   304 
   305 
   345 rejects the focus change. See <a href="#FocusAndVetoableChangeListener">Focus
   346 rejects the focus change. See <a href="#FocusAndVetoableChangeListener">Focus
   346 and VetoableChangeListener</a>.
   347 and VetoableChangeListener</a>.
   347 
   348 
   348 
   349 
   349 <a id="OppositeComponents"></a>
   350 <a id="OppositeComponents"></a>
   350 <h3>Opposite Components and Windows</h3>
   351 <h2>Opposite Components and Windows</h2>
   351 <p>
   352 <p>
   352 Each event includes information about the "opposite" Component or
   353 Each event includes information about the "opposite" Component or
   353 Window involved in the focus or activation change. For example, for a
   354 Window involved in the focus or activation change. For example, for a
   354 <code>FOCUS_GAINED</code> event, the opposite Component is the Component
   355 <code>FOCUS_GAINED</code> event, the opposite Component is the Component
   355 that lost focus. If the focus or activation change occurs with a native
   356 that lost focus. If the focus or activation change occurs with a native
   369 a pure Swing application can ignore this platform restriction when
   370 a pure Swing application can ignore this platform restriction when
   370 using the opposite Component of a focus change that occurred within a
   371 using the opposite Component of a focus change that occurred within a
   371 top-level Window.
   372 top-level Window.
   372 
   373 
   373 <a id="TemporaryFocusEvents"></a>
   374 <a id="TemporaryFocusEvents"></a>
   374 <h3>Temporary FocusEvents</h3>
   375 <h2>Temporary FocusEvents</h2>
   375 <p>
   376 <p>
   376 <code>FOCUS_GAINED</code> and <code>FOCUS_LOST</code> events are
   377 <code>FOCUS_GAINED</code> and <code>FOCUS_LOST</code> events are
   377 marked as either temporary or permanent.
   378 marked as either temporary or permanent.
   378 <p>
   379 <p>
   379 Temporary <code>FOCUS_LOST</code> events are sent when a Component is
   380 Temporary <code>FOCUS_LOST</code> events are sent when a Component is
   414 behavior for this method can be guaranteed only for lightweight
   415 behavior for this method can be guaranteed only for lightweight
   415 Components. This method is not intended for general use, but exists
   416 Components. This method is not intended for general use, but exists
   416 instead as a hook for lightweight Component libraries, such as Swing.
   417 instead as a hook for lightweight Component libraries, such as Swing.
   417 
   418 
   418 <a id="FocusTraversal"></a>
   419 <a id="FocusTraversal"></a>
   419 <h3>Focus Traversal</h3>
   420 <h2>Focus Traversal</h2>
   420 <p>
   421 <p>
   421 Each Component defines its own Set of focus traversal keys for a given
   422 Each Component defines its own Set of focus traversal keys for a given
   422 focus traversal operation. Components support separate Sets of keys
   423 focus traversal operation. Components support separate Sets of keys
   423 for forward and backward traversal, and also for traversal up one
   424 for forward and backward traversal, and also for traversal up one
   424 focus traversal cycle. Containers which are focus cycle roots also
   425 focus traversal cycle. Containers which are focus cycle roots also
   489 current focus owner. If the current focus owner is not a focus cycle
   490 current focus owner. If the current focus owner is not a focus cycle
   490 root, then no focus traversal operation occurs.
   491 root, then no focus traversal operation occurs.
   491 
   492 
   492 
   493 
   493 <a id="FocusTraversalPolicy"></a>
   494 <a id="FocusTraversalPolicy"></a>
   494 <h3>FocusTraversalPolicy</h3>
   495 <h2>FocusTraversalPolicy</h2>
   495 <p>
   496 <p>
   496 
   497 
   497 A <code>FocusTraversalPolicy</code> defines the order in which Components within
   498 A <code>FocusTraversalPolicy</code> defines the order in which Components within
   498 a particular focus cycle root or focus traversal policy provider are
   499 a particular focus cycle root or focus traversal policy provider are
   499 traversed. Instances of <code>FocusTraversalPolicy</code> can be shared across
   500 traversed. Instances of <code>FocusTraversalPolicy</code> can be shared across
   651 <p>
   652 <p>
   652 All other applications, including pure AWT applications, will use
   653 All other applications, including pure AWT applications, will use
   653 <code>DefaultFocusTraversalPolicy</code> by default.
   654 <code>DefaultFocusTraversalPolicy</code> by default.
   654 
   655 
   655 <a id="FocusTraversalPolicyProviders"></a>
   656 <a id="FocusTraversalPolicyProviders"></a>
   656 <h3>Focus Traversal Policy Providers</h3>
   657 <h2>Focus Traversal Policy Providers</h2>
   657 <p>
   658 <p>
   658   A Container that isn't a focus cycle root has an option to provide a
   659   A Container that isn't a focus cycle root has an option to provide a
   659   FocusTraversalPolicy of its own. To do so, one needs to set Container's focus
   660   FocusTraversalPolicy of its own. To do so, one needs to set Container's focus
   660   traversal policy provider property to <code>true</code> with the call to
   661   traversal policy provider property to <code>true</code> with the call to
   661 
   662 
   738                that provider is returned
   739                that provider is returned
   739         </ul>
   740         </ul>
   740   </ul>
   741   </ul>
   741 
   742 
   742 <a id="ProgrammaticTraversal"></a>
   743 <a id="ProgrammaticTraversal"></a>
   743 <h3>Programmatic Traversal</h3>
   744 <h2>Programmatic Traversal</h2>
   744 <p>
   745 <p>
   745 In addition to user-initiated focus traversal, client code can
   746 In addition to user-initiated focus traversal, client code can
   746 initiate a focus traversal operation programmatically. To client code,
   747 initiate a focus traversal operation programmatically. To client code,
   747 programmatic traversals are indistinguishable from user-initiated
   748 programmatic traversals are indistinguishable from user-initiated
   748 traversals. The preferred way to initiate a programmatic traversal is
   749 traversals. The preferred way to initiate a programmatic traversal is
   809 and there is no other Component to focus, then the focus owner remains
   810 and there is no other Component to focus, then the focus owner remains
   810 unchanged.
   811 unchanged.
   811 
   812 
   812 
   813 
   813 <a id="Focusability"></a>
   814 <a id="Focusability"></a>
   814 <h3>Focusability</h3>
   815 <h2>Focusability</h2>
   815 <p>
   816 <p>
   816 A focusable Component can become the focus owner ("focusability") and
   817 A focusable Component can become the focus owner ("focusability") and
   817 participates in keyboard focus traversal ("focus traversability") with
   818 participates in keyboard focus traversal ("focus traversability") with
   818 a FocusTraversalPolicy. There is no separation of these two concepts;
   819 a FocusTraversalPolicy. There is no separation of these two concepts;
   819 a Component must either be both focusable and focus traversable, or
   820 a Component must either be both focusable and focus traversable, or
   823 default, all Components return true from this method. Client code can
   824 default, all Components return true from this method. Client code can
   824 change this default by calling Component.setFocusable(boolean).
   825 change this default by calling Component.setFocusable(boolean).
   825 
   826 
   826 
   827 
   827 <a id="FocusableWindows"></a>
   828 <a id="FocusableWindows"></a>
   828 <h3>Focusable Windows</h3>
   829 <h2>Focusable Windows</h2>
   829 <p>
   830 <p>
   830 To support palette windows and input methods, client code can prevent
   831 To support palette windows and input methods, client code can prevent
   831 a Window from becoming the focused Window. By transitivity, this
   832 a Window from becoming the focused Window. By transitivity, this
   832 prevents the Window or any of its descendants from becoming the focus
   833 prevents the Window or any of its descendants from becoming the focus
   833 owner. Non-focusable Windows may still own Windows that are
   834 owner. Non-focusable Windows may still own Windows that are
   875 <a href=#RequestingFocus>Requesting Focus</a>), it is possible that
   876 <a href=#RequestingFocus>Requesting Focus</a>), it is possible that
   876 all such focus change requests will fail. In this case, the global
   877 all such focus change requests will fail. In this case, the global
   877 focus owner will be cleared and the focused Window will remain unchanged.
   878 focus owner will be cleared and the focused Window will remain unchanged.
   878 
   879 
   879 <a id="RequestingFocus"></a>
   880 <a id="RequestingFocus"></a>
   880 <h3>Requesting Focus</h3>
   881 <h2>Requesting Focus</h2>
   881 
   882 
   882 <p>
   883 <p>
   883 A Component can request that it become the focus owner by calling
   884 A Component can request that it become the focus owner by calling
   884 <code>Component.requestFocus()</code>. This initiates a permanent
   885 <code>Component.requestFocus()</code>. This initiates a permanent
   885 focus transfer to the Component only if the Component is displayable,
   886 focus transfer to the Component only if the Component is displayable,
   947 <code>requestFocusInWindow</code> that allow client code to specify
   948 <code>requestFocusInWindow</code> that allow client code to specify
   948 a temporary state.
   949 a temporary state.
   949 See <a href="#TemporaryFocusEvents">Temporary FocusEvents</a>
   950 See <a href="#TemporaryFocusEvents">Temporary FocusEvents</a>
   950 
   951 
   951 <a id="FocusAndPropertyChangeListener"></a>
   952 <a id="FocusAndPropertyChangeListener"></a>
   952 <h3>Focus and PropertyChangeListener</h3>
   953 <h2>Focus and PropertyChangeListener</h2>
   953 <p>
   954 <p>
   954 Client code can listen to changes in context-wide focus state, or to
   955 Client code can listen to changes in context-wide focus state, or to
   955 changes in focus-related state in Components, via
   956 changes in focus-related state in Components, via
   956 PropertyChangeListeners.
   957 PropertyChangeListeners.
   957 <p>
   958 <p>
  1018 Window will never see a <code>PropertyChangeEvent</code> for the
  1019 Window will never see a <code>PropertyChangeEvent</code> for the
  1019 <code>focusCycleRoot</code> property.
  1020 <code>focusCycleRoot</code> property.
  1020 A Window is always a focus cycle root; this property cannot change.
  1021 A Window is always a focus cycle root; this property cannot change.
  1021 <p>
  1022 <p>
  1022 <a id="FocusAndVetoableChangeListener"></a>
  1023 <a id="FocusAndVetoableChangeListener"></a>
  1023 <h3>Focus and VetoableChangeListener</h3>
  1024 <h2>Focus and VetoableChangeListener</h2>
  1024 <p>
  1025 <p>
  1025 The <code>KeyboardFocusManager</code> also supports
  1026 The <code>KeyboardFocusManager</code> also supports
  1026 <code>VetoableChangeListener</code>s for the following properties:
  1027 <code>VetoableChangeListener</code>s for the following properties:
  1027 
  1028 
  1028   <ol>
  1029   <ol>
  1087 to anticipate this situation could lead to an infinite cycle of
  1088 to anticipate this situation could lead to an infinite cycle of
  1088 vetoed focus changes and recovery attempts.
  1089 vetoed focus changes and recovery attempts.
  1089 
  1090 
  1090 
  1091 
  1091 <a id="ZOrder"></a>
  1092 <a id="ZOrder"></a>
  1092 <h3>Z-Order</h3>
  1093 <h2>Z-Order</h2>
  1093 <p>
  1094 <p>
  1094 On some native windowing systems, the Z-order of a Window can affect
  1095 On some native windowing systems, the Z-order of a Window can affect
  1095 its focused or active (if applicable) state. On Microsoft Windows, the
  1096 its focused or active (if applicable) state. On Microsoft Windows, the
  1096 top-most Window is naturally the focused Window as well. However, on
  1097 top-most Window is naturally the focused Window as well. However, on
  1097 Solaris, many window managers use a point-to-focus model that ignores
  1098 Solaris, many window managers use a point-to-focus model that ignores
  1167         manager. The window may be in a native application, or a Java
  1168         manager. The window may be in a native application, or a Java
  1168         application in another VM.
  1169         application in another VM.
  1169   </ul>
  1170   </ul>
  1170 
  1171 
  1171 <a id="ReplacingDefaultKeyboardFocusManager"></a>
  1172 <a id="ReplacingDefaultKeyboardFocusManager"></a>
  1172 <h3>Replacing DefaultKeyboardFocusManager</h3>
  1173 <h2>Replacing DefaultKeyboardFocusManager</h2>
  1173 <p>
  1174 <p>
  1174 <code>KeyboardFocusManager</code>s are pluggable at the browser context
  1175 <code>KeyboardFocusManager</code>s are pluggable at the browser context
  1175 level. Client code can subclass <code>KeyboardFocusManager</code> or
  1176 level. Client code can subclass <code>KeyboardFocusManager</code> or
  1176 <code>DefaultKeyboardFocusManager</code> to modify the way that WindowEvents
  1177 <code>DefaultKeyboardFocusManager</code> to modify the way that WindowEvents
  1177 related to focus, FocusEvents, and KeyEvents are handled and
  1178 related to focus, FocusEvents, and KeyEvents are handled and
  1323           implementation of the Window class may generate these spurious
  1324           implementation of the Window class may generate these spurious
  1324           events.
  1325           events.
  1325     </ul>
  1326     </ul>
  1326 
  1327 
  1327 <a id="Incompatibilities"></a>
  1328 <a id="Incompatibilities"></a>
  1328 <h3>Incompatibilities with Previous Releases</h3>
  1329 <h2>Incompatibilities with Previous Releases</h2>
  1329   <p><b>Cross-platform changes:</b>
  1330   <p><b>Cross-platform changes:</b>
  1330     <ol>
  1331     <ol>
  1331       <li>The default focus traversability for all Components is now
  1332       <li>The default focus traversability for all Components is now
  1332           'true'. Previously, some Components (in particular, all
  1333           'true'. Previously, some Components (in particular, all
  1333           lightweights), had a default focus traversability of 'false'. Note
  1334           lightweights), had a default focus traversability of 'false'. Note
  1359           the top-most Window after the Z-order change.
  1360           the top-most Window after the Z-order change.
  1360       <li><code>requestFocus()</code> now allows cross-Window focus
  1361       <li><code>requestFocus()</code> now allows cross-Window focus
  1361           change requests in all cases. Previously, requests were granted
  1362           change requests in all cases. Previously, requests were granted
  1362           for heavyweights, but denied for lightweights.
  1363           for heavyweights, but denied for lightweights.
  1363     </ol>
  1364     </ol>
  1364 
  1365   </main>
  1365 </body>
  1366 </body>
  1366 </html>
  1367 </html>