1 <!doctype html>
   2 <html lang="en">
   3 <head>
   4   <meta charset="utf-8"/>
   5   <title>The AWT Modality</title>
   6 </head>
   7 <!--
   8  Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
   9  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  10 
  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
  13  published by the Free Software Foundation.  Oracle designates this
  14  particular file as subject to the "Classpath" exception as provided
  15  by Oracle in the LICENSE file that accompanied this code.
  16 
  17  This code is distributed in the hope that it will be useful, but WITHOUT
  18  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  20  version 2 for more details (a copy is included in the LICENSE file that
  21  accompanied this code).
  22 
  23  You should have received a copy of the GNU General Public License version
  24  2 along with this work; if not, write to the Free Software Foundation,
  25  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  26 
  27  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  28  or visit www.oracle.com if you need additional information or have any
  29  questions.
  30 -->
  31 
  32 <body>
  33 
  34     <h1>The AWT Modality</h1>
  35 
  36     <p>
  37       This document, together with the API documentation for modality-related
  38       classes (such as <code>java.awt.Dialog</code>), briefly describes the new
  39       modality features and how to use them. It contains the following sections:
  40     </p><ul>
  41       <li><a href="#Definitions">Definitions</a></li>
  42       <li><a href="#ModalityTypes">Modality types</a></li>
  43       <li><a href="#ShowHideBlocking">Show/hide blocking</a></li>
  44       <li><a href="#ModalExclusion">Modal exclusion</a></li>
  45       <li><a href="#Related">Related AWT features</a></li>
  46       <li><a href="#Security">Security</a></li>
  47       <li><a href="#PlatformSupport">Platform support</a></li>
  48       <li><a href="#Compatibility">Compatibility</a></li>
  49       <li><a href="#Examples">Examples</a></li>
  50     </ul>
  51 
  52     <a id="Definitions"></a>
  53     <h3>Definitions</h3>
  54 
  55     <p>
  56       <u>Document</u> - a window without an owner that, together with
  57       all its child hierarchy, may be operated on as a single self-contained
  58       document.
  59       Every window belongs to some document &mdash; its root can be found as
  60       the closest ancestor window without an owner.
  61     </p><p>
  62       <a id="ModalBlocked"></a>
  63       <u>Modal blocked window</u> - a window, that:
  64       </p><ul>
  65         <li>doesn't receive any user input events
  66         </li><li>doesn't receive input focus
  67         </li><li>keeps its Z-order below the modal dialog that blocks it
  68       </li></ul>
  69       <blockquote>
  70         <hr>
  71           <b>Warning!</b> Some window managers allow users to change the window
  72           Z-order in an arbitrary way &mdash; in that case the last requirement
  73           may not be met.
  74         <hr>
  75       </blockquote>
  76     <p>
  77       <u>Modal dialog</u> - a dialog that blocks some windows while it is
  78       visible. The blocked windows are determined according to the dialog's
  79       scope of blocking.
  80     </p><p>
  81       <u>Modal excluded window</u> - a window that stays unblocked
  82       while the modal dialog is visible. If a window is modal excluded
  83       then all its owned windows and child components are also excluded.
  84     </p><p>
  85       <u>Scope of blocking (SB)</u> - the set of windows (instances of
  86       <code>java.awt.Window</code> and all derived classes) that are blocked by
  87       the modal dialog while it is visible.
  88      <blockquote><hr>
  89       <b>Note</b>: Everywhere in this document the notion of "window" is equal
  90       to a top-level window in the Java programming language &mdash; in other words
  91       an instance of <code>java.awt.Window</code> or any descendant class.
  92       <hr></blockquote>
  93 
  94     <a id="ModalityTypes"></a>
  95     <h3>Modality types</h3>
  96 
  97     <p>
  98       There are four supported modality types :
  99       </p><ul>
 100         <li>toolkit
 101         </li><li>application
 102         </li><li>document
 103         </li><li>modeless
 104       </li></ul>
 105       A dialog is, by default, modeless.  A modal dialog is, by default,
 106       application-modal.
 107     <ol>
 108       <li><u>Modeless dialogs</u><br>
 109         A modeless dialog doesn't block any windows while visible.
 110       </li><li><u>Document-modal dialogs</u><br>
 111         A document-modal dialog blocks all windows from the same
 112         document except those from its child hierarchy. The document root
 113         is determined as the closest ancestor window without an
 114         owner.
 115       </li><li><u>Application-modal dialogs</u><br>
 116         An application-modal dialog blocks all windows from the same
 117         application except for those from its child hierarchy.
 118         If there are several applets launched in a browser, they can be
 119         treated either as separate applications or a single application.
 120         This behavior is implementation-dependent.
 121       </li><li><u>Toolkit-modal dialogs</u><br>
 122         A toolkit-modal dialog blocks all windows that run in the same
 123         toolkit except those from its child hierarchy. If there
 124         are several applets launched all of them run with the same toolkit,
 125         so a toolkit-modal dialog shown from an applet may affect other
 126         applets and all windows of the browser instance which embeds the
 127         Java runtime environment for this toolkit.
 128         See the security section below.
 129     </li></ol>
 130     <p>
 131       Modality priority is arranged by the strength of blocking: modeless,
 132       document-modal, application-modal and toolkit-modal. This arrangement
 133       is used when determining what dialog should remain unblocked if two
 134       are visible and block each other. It naturally reflects the nesting
 135       of a dialog's scope of blocking (SB): a modeless dialog has an empty SB,
 136       a document-modal dialog's SB is complete in some applications,
 137       and all the applications are run in one toolkit.  </p><p>
 138       Notes about owners:
 139       </p><ul>
 140         <li>Creating a document-modal dialog without an owner:<br>
 141           Since <code>Dialog</code> is a class derived from
 142           <code>Window</code>, a <code>Dialog</code> instance automatically
 143           becomes the root of the document if it has no owner. Thus, if
 144           such a dialog is document-modal, its scope of blocking is empty
 145           and it behaves the same way as a modeless dialog.
 146         </li><li>Creating an application-modal or toolkit-modal dialog with an
 147           owner:<br>
 148           The scope of blocking for an application- or toolkit-modal
 149           dialog, as opposed to a document-modal dialog, doesn't depend on
 150           its owner. Thus, in this case the only thing that the owner
 151           affects is the Z-order: the dialog always stays on top of its owner.
 152       </li></ul>
 153     <blockquote><hr>
 154       <b>Implementation note</b>: Changing the modality type for a visible
 155       dialog may have no effect until it is hidden and then shown again.
 156       <hr></blockquote>
 157 
 158     <a id="ShowHideBlocking"></a>
 159     <h3>Show/hide blocking</h3>
 160 
 161     <p>
 162       <u>Showing the window or modeless dialog: "F"</u><br>
 163       All the visible modal dialogs are looked through &mdash; if F is from the SB
 164       of one of them, it becomes blocked by it. If there are several such
 165       dialogs, the first shown is used. If no such dialogs exist, F remains
 166       unblocked.
 167     </p><p>
 168       <u>Showing the modal dialog: "M"</u><br>
 169       When modal dialog M is shown, all the visible windows fall into one of
 170       three distinct groups:
 171       <ul>
 172       <li>Blockers of M (modal dialogs that block M and
 173       either are in M's child hierarchy, or are not blocked by M, or have
 174       a greater mode of modality, or block some other blocker of M)
 175       <li>Blocked by M (windows from M's SB that are not blockers and are
 176       not in child hierarchy of any blocker)
 177       <li>All other windows (windows or modeless
 178       dialogs outside M's SB and modal dialogs outside M's SB that do not
 179       block M).
 180       </ul>
 181       <p>
 182       After the modal dialog M is shown, it becomes blocked by the first shown
 183       dialog from the first group (if there are any), all the windows from the
 184       second one become blocked by M, and all the windows from the third group
 185       remain untouched.
 186     </p><p>
 187       <u>In typical cases</u>, when no child dialogs are shown before their owners,
 188       this rule can be simplified. (The following, simplified case, may
 189       leave out some details).
 190       </p><p>
 191       <u>Showing the document-modal dialog: "M"</u><br>
 192       All the visible application- and toolkit-modal dialogs are looked
 193       through &mdash; if M is from the SB of one of them,
 194       it becomes blocked by it. If there are several such dialogs,
 195       the first shown is used. If no such dialogs exist, M remains unblocked.
 196       </p><p>
 197       <u>Showing the application-modal dialog: "M"</u><br>
 198       All the visible toolkit-modal dialogs are looked through &mdash;
 199       if M is from the SB of one of them, it becomes blocked by it.
 200       If there are several such dialogs, the first shown is used.
 201       If no such dialogs exist, M remains unblocked.
 202       </p><p>
 203       <u>Showing the toolkit-modal dialog: "M"</u><br>
 204       M remains unblocked.
 205       </p><p>
 206 <!--        <center> -->
 207           </p>
 208           <table border="1">
 209           <caption>The Standard Blocking Matrix</caption>
 210             <tbody><tr align="center">
 211               <td align="center">current/shown</td>
 212               <td align="center">frame &amp; modeless</td>
 213               <td align="center">document</td>
 214               <td align="center">application</td>
 215               <td align="center">toolkit</td>
 216             </tr>
 217             <tr align="center">
 218               <td align="center">-</td>
 219               <td align="center">-</td>
 220               <td align="center">-</td>
 221               <td align="center">-</td>
 222               <td align="center">-</td>
 223             </tr>
 224             <tr align="center">
 225               <td align="center">document</td>
 226               <td align="center">blocked</td>
 227               <td align="center">-</td>
 228               <td align="center">-</td>
 229               <td align="center">-</td>
 230             </tr>
 231             <tr align="center">
 232               <td align="center">application</td>
 233               <td align="center">blocked</td>
 234               <td align="center">blocked</td>
 235               <td align="center">-</td>
 236               <td align="center">-</td>
 237             </tr>
 238             <tr align="center">
 239               <td align="center">toolkit</td>
 240               <td align="center">blocked</td>
 241               <td align="center">blocked</td>
 242               <td align="center">blocked</td>
 243               <td align="center">-</td>
 244             </tr>
 245           </tbody></table>
 246 <!--        </center> -->
 247       <p>
 248       After the modal dialog is shown, all the windows from its SB are blocked,
 249       except those that block this modal dialog.
 250     </p><p>
 251       <u>Hiding the window or modeless dialog: "F"</u><br>
 252       If F was blocked by any modal dialog M, it becomes unblocked and is
 253       removed from M's blocked windows list.
 254     </p><p>
 255       <u>Hiding the modal dialog: "M"</u><br>
 256       If M was blocked by any other modal dialog, for example, "N",
 257       it becomes unblocked and
 258       is removed from N's blocked windows list. Then, all the windows and dialogs
 259       blocked by M become unblocked, and after that the same checks
 260       (as in Showing the modal dialog: "M")
 261       are performed for each of them in the order they were initially shown.
 262 
 263     <a id="ModalExclusion"></a>
 264     </p><h3>Modal exclusion</h3>
 265 
 266     <p>
 267       There are two modal exclusion types introduced as of JDK 6
 268       </p><ul>
 269         <li>Exclusion from blocking of toolkit-modal dialogs
 270         </li><li>Exclusion from blocking of application-modal dialogs
 271       </li></ul>
 272       By default, a window's modal exclusion property is turned off.
 273       <ol>
 274         <li><u>Application-modal exclusion</u><br>
 275           If a window is application-modal excluded, it is not blocked by any
 276           application-modal dialogs. Also, it is not blocked by document-modal
 277           dialogs from outside of its child hierarchy.
 278         </li><li><u>Toolkit-modal exclusion</u><br>
 279           If a window is toolkit-modal excluded, it is not blocked
 280           by any application- or toolkit-modal dialogs. Also, it is not
 281           blocked by document-modal dialogs from outside of their child hierarchy.
 282       </li></ol>
 283     <blockquote>
 284       <hr>
 285         <b>Implementation note</b>: Changing the modal exclusion type for a visible window
 286         may have no effect until it is hidden and then shown again.
 287     </blockquote>
 288 
 289     <a id="Related"></a>
 290     <h3>Related AWT features</h3>
 291 
 292     <p>
 293       <u>Always-On-Top</u><br>
 294       When a modal dialog that is not always-on-top blocks an always-on-top window,
 295       their relative Z-order is unspecified and platform-dependent.
 296     </p>
 297     <p>
 298       <u>The <code>toFront()</code> and <code>toBack()</code> methods</u><br>
 299       A modal dialog should always be above all its blocked windows. Thus, if a blocked
 300       window is brought to the front, its blocking dialog, if any, is also brought to the
 301       front and remains above the blocked window. Likewise, if a modal dialog is sent to
 302       the back, all of its blocked windows are sent to the back to keep them below the
 303       blocking dialog.
 304     </p>
 305     <p>
 306       <u>Minimizing, maximizing and closing blocked windows</u><br>
 307       When a modal dialog blocks a window, the user may not be able to maximize or
 308       minimize the blocked window&mdash; however, the actual behavior is unspecified
 309       and platform-dependent. In any case, the user can't close the blocked window
 310       interactively&mdash; but it can be closed programmatically by calling the
 311       <code>setVisible(false)</code> or <code>dispose()</code> methods on the blocked
 312       window.
 313     </p>
 314     <p>
 315       <u>Blocked windows activations</u><br>
 316       When the user selects a blocked window, it may be brought to the front, along
 317       with the blocking modal dialog which would then become the active window&mdash;
 318       however, the actual behavior is unspecified and platform-dependent.
 319     </p>
 320     <p>
 321       <u>Hiding a modal dialog</u><br>
 322       When the modal dialog that currently has focus is hidden, it is unspecified
 323       and platform-dependent, which other window will become the active window.
 324       Any of the following may become the active window:
 325       <ol>
 326         <li>The owner of the modal dialog - if the owner is unblocked.
 327         </li><li>The <code>Window</code>, which was active before this modal dialog gained
 328         focus - if the owner of the modal dialog is absent or is blocked.
 329       </li></ol>
 330       If the modal dialog to be hidden does not have focus, the active window remains
 331       unchanged.
 332 
 333     <a id="Security"></a>
 334     <h3>Security</h3>
 335 
 336     <p>
 337       A special <code>AWTPermission</code>, <code>"toolkitModality"</code>,
 338       is required to show toolkit-modal
 339       dialogs. This would prevent, for example, blocking a browser or
 340       Java Web Start (JWS) by modal dialogs shown from applets.
 341     </p><p>
 342       The same permission is required to exclude a window from toolkit modality.
 343       This would prevent, for example, a dialog shown from an applet not to be
 344       blocked by a browser's or JWS's modal dialog.
 345 
 346     <a id="PlatformSupport"></a>
 347     </p><h3>Platform support</h3>
 348 
 349     <p>
 350       Two <code>java.awt.Toolkit</code> methods allow you to check whether
 351       the current platform supports specific modality features:
 352       </p><ul>
 353         <li><code>isModalityTypeSupported(modalityType)</code><br>
 354           Returns whether the specified modality type is supported on
 355           the current platform.
 356           If mode "M" is not supported and a dialog is set to M-modal,
 357           it behaves as modeless.
 358         </li>
 359         <li><code>isModalExclusionTypeSupported(modalExclusionType)</code><br>
 360           Returns whether the given modal exclusion type is supported on
 361           the current platform. If exclusion type "E" is not supported
 362           and a window is marked as E-excluded, this has no effect.
 363       </li></ul>
 364 
 365     <a id="Compatibility"></a>
 366     <h3>Compatibility</h3>
 367 
 368     <p>
 369       The default modality type is application-modal. It is used by the API
 370       calls: <code>Dialog.setModal(true)</code>,
 371       <code>Dialog(owner, true)</code>, etc. Prior to JDK 6
 372       the default type was toolkit-modal,
 373       but the only distinction between application- and toolkit-modality is for
 374       applets and applications launched from Java Web Start.
 375 
 376     <a id="Examples"></a>
 377     </p><h3>Examples</h3>
 378 
 379     <table border="0">
 380       <tbody><tr>
 381         <td align="left" >
 382         <ol>
 383           <li>Frame "F" is shown<br>
 384           <li>Document-modal dialog "D<sub>i</sub>" is shown<br>
 385           <li>F becomes blocked by D<sub>i</sub> &mdash; it's in the same document<br>
 386           <li>Document-modal dialog "D<sub>ii</sub>" is shown<br>
 387           <li>D<sub>i</sub> becomes blocked by D<sub>ii</sub> &mdash; it's in the
 388               same document<br>
 389           </ol>
 390           <br>
 391         </td>
 392         <td align="center">
 393           <img src="modal-example1.gif" alt="Example 1">
 394           <br>
 395         </td>
 396       </tr>
 397       <tr>
 398         <td align="left">
 399         <ol>
 400          <li>Frame "F" is shown<br>
 401          <li>Document-modal dialog "D<sub>i</sub>" is shown<br>
 402          <li>F becomes blocked by D<sub>i</sub> &mdash; it's in the same document<br>
 403          <li>Document-modal dialog "D<sub>ii</sub>" is shown<br>
 404          <li>D<sub>i</sub> becomes blocked by D<sub>ii</sub> &mdash;
 405              it's in the same document<br>
 406          <li>D<sub>i</sub> is hidden<br>
 407          <li>F becomes blocked by D<sub>ii</sub> &mdash; it's in the same document<br>
 408          </ol>
 409          <br>
 410         </td>
 411         <td align="center">
 412           <img src="modal-example2.gif" alt="Example 2">
 413           <br>
 414         </td>
 415       </tr>
 416       <tr>
 417         <td align="left">
 418         <ol>
 419           <li>Frame "F" is shown<br>
 420           <li>Toolkit-modal dialog "D<sub>i</sub>" is created, but not shown<br>
 421           <li>Document-modal dialog "D<sub>ii</sub>" is shown<br>
 422           <li>F becomes blocked by D<sub>ii</sub> &mdash; it's in the same document<br>
 423           <li>Application-modal dialog "D<sub>iii</sub>" is shown<br>
 424           <li>D<sub>ii</sub> becomes blocked by D<sub>iii</sub> &mdash;
 425               it's in the same application<br>
 426           <li>D<sub>i</sub> is shown<br>
 427           <li>D<sub>i</sub> becomes blocked by D<sub>ii</sub> &mdash; it's its owner<br>
 428           <li>D<sub>iii</sub> remains unblocked &mdash; it blocks D<sub>ii</sub> and
 429               D<sub>ii</sub> blocks D<sub>i</sub><br>
 430           </ol>
 431           <br>
 432         </td>
 433         <td align="center">
 434           <img src="modal-example3.gif" alt="Example 3">
 435           <br>
 436         </td>
 437       </tr>
 438       <tr>
 439         <td align="left">
 440         <ol>
 441           <li>Frame "F" is shown<br>
 442           <li>Toolkit-modal dialog "D<sub>i</sub>" is created, but not shown<br>
 443           <li>Document-modal dialog "D<sub>ii</sub>" is shown<br>
 444           <li>F becomes blocked by D<sub>ii</sub> &mdash; it's in the same document<br>
 445           <li>Application-modal dialog "D<sub>iii</sub>" is shown<br>
 446           <li>D<sub>ii</sub> becomes blocked by D<sub>iii</sub> &mdash; it's in the
 447               same application<br>
 448           <li>D<sub>i</sub> is shown<br>
 449           <li>D<sub>iii</sub> becomes blocked by D<sub>i</sub> &mdash; D<sub>i</sub>
 450               is not blocked<br>
 451           <li>D<sub>i</sub> remains unblocked<br>
 452           </ol>
 453           <br>
 454         </td>
 455         <td align="center">
 456           <img src="modal-example4.gif" alt="Example 4">
 457           <br>
 458         </td>
 459       </tr>
 460     </tbody></table>
 461 
 462 </body></html>