1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.awt.*; 28 import java.awt.event.*; 29 import javax.accessibility.*; 30 31 /** 32 * The main class for creating a dialog window. You can use this class 33 * to create a custom dialog, or invoke the many class methods 34 * in {@link JOptionPane} to create a variety of standard dialogs. 35 * For information about creating dialogs, see 36 * <em>The Java Tutorial</em> section 37 * <a 38 href="http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html">How 39 * to Make Dialogs</a>. 40 * 41 * <p> 42 * 43 * The {@code JDialog} component contains a {@code JRootPane} 44 * as its only child. 45 * The {@code contentPane} should be the parent of any children of the 46 * {@code JDialog}. 47 * As a convenience, the {@code add}, {@code remove}, and {@code setLayout} 48 * methods of this class are overridden, so that they delegate calls 49 * to the corresponding methods of the {@code ContentPane}. 50 * For example, you can add a child component to a dialog as follows: 51 * <pre> 52 * dialog.add(child); 53 * </pre> 54 * And the child will be added to the contentPane. 55 * The {@code contentPane} is always non-{@code null}. 56 * Attempting to set it to {@code null} generates an exception. 57 * The default {@code contentPane} has a {@code BorderLayout} 58 * manager set on it. 59 * Refer to {@link javax.swing.RootPaneContainer} 60 * for details on adding, removing and setting the {@code LayoutManager} 61 * of a {@code JDialog}. 62 * <p> 63 * Please see the {@code JRootPane} documentation for a complete 64 * description of the {@code contentPane}, {@code glassPane}, 65 * and {@code layeredPane} components. 66 * <p> 67 * In a multi-screen environment, you can create a {@code JDialog} 68 * on a different screen device than its owner. See {@link java.awt.Frame} for 69 * more information. 70 * <p> 71 * <strong>Warning:</strong> Swing is not thread safe. For more 72 * information see <a 73 * href="package-summary.html#threading">Swing's Threading 74 * Policy</a>. 75 * <p> 76 * <strong>Warning:</strong> 77 * Serialized objects of this class will not be compatible with 78 * future Swing releases. The current serialization support is 79 * appropriate for short term storage or RMI between applications running 80 * the same version of Swing. As of 1.4, support for long term storage 81 * of all JavaBeans™ 82 * has been added to the {@code java.beans} package. 83 * Please see {@link java.beans.XMLEncoder}. 84 * 85 * @see JOptionPane 86 * @see JRootPane 87 * @see javax.swing.RootPaneContainer 88 * 89 * @beaninfo 90 * attribute: isContainer true 91 * attribute: containerDelegate getContentPane 92 * description: A toplevel window for creating dialog boxes. 93 * 94 * @author David Kloba 95 * @author James Gosling 96 * @author Scott Violet 97 */ 98 @SuppressWarnings("serial") // Same-version serialization only 99 public class JDialog extends Dialog implements WindowConstants, 100 Accessible, 101 RootPaneContainer, 102 TransferHandler.HasGetTransferHandler 103 { 104 /** 105 * Key into the AppContext, used to check if should provide decorations 106 * by default. 107 */ 108 private static final Object defaultLookAndFeelDecoratedKey = 109 new StringBuffer("JDialog.defaultLookAndFeelDecorated"); 110 111 private int defaultCloseOperation = HIDE_ON_CLOSE; 112 113 /** 114 * @see #getRootPane 115 * @see #setRootPane 116 */ 117 protected JRootPane rootPane; 118 119 /** 120 * If true then calls to {@code add} and {@code setLayout} 121 * will be forwarded to the {@code contentPane}. This is initially 122 * false, but is set to true when the {@code JDialog} is constructed. 123 * 124 * @see #isRootPaneCheckingEnabled 125 * @see #setRootPaneCheckingEnabled 126 * @see javax.swing.RootPaneContainer 127 */ 128 protected boolean rootPaneCheckingEnabled = false; 129 130 /** 131 * The {@code TransferHandler} for this dialog. 132 */ 133 private TransferHandler transferHandler; 134 135 /** 136 * Creates a modeless dialog without a title and without a specified 137 * {@code Frame} owner. A shared, hidden frame will be 138 * set as the owner of the dialog. 139 * <p> 140 * This constructor sets the component's locale property to the value 141 * returned by {@code JComponent.getDefaultLocale}. 142 * <p> 143 * NOTE: This constructor does not allow you to create an unowned 144 * {@code JDialog}. To create an unowned {@code JDialog} 145 * you must use either the {@code JDialog(Window)} or 146 * {@code JDialog(Dialog)} constructor with an argument of 147 * {@code null}. 148 * 149 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 150 * returns {@code true}. 151 * @see java.awt.GraphicsEnvironment#isHeadless 152 * @see JComponent#getDefaultLocale 153 */ 154 public JDialog() { 155 this((Frame)null, false); 156 } 157 158 /** 159 * Creates a modeless dialog with the specified {@code Frame} 160 * as its owner and an empty title. If {@code owner} 161 * is {@code null}, a shared, hidden frame will be set as the 162 * owner of the dialog. 163 * <p> 164 * This constructor sets the component's locale property to the value 165 * returned by {@code JComponent.getDefaultLocale}. 166 * <p> 167 * NOTE: This constructor does not allow you to create an unowned 168 * {@code JDialog}. To create an unowned {@code JDialog} 169 * you must use either the {@code JDialog(Window)} or 170 * {@code JDialog(Dialog)} constructor with an argument of 171 * {@code null}. 172 * 173 * @param owner the {@code Frame} from which the dialog is displayed 174 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 175 * returns {@code true}. 176 * @see java.awt.GraphicsEnvironment#isHeadless 177 * @see JComponent#getDefaultLocale 178 */ 179 public JDialog(Frame owner) { 180 this(owner, false); 181 } 182 183 /** 184 * Creates a dialog with an empty title and the specified modality and 185 * {@code Frame} as its owner. If {@code owner} is {@code null}, 186 * a shared, hidden frame will be set as the owner of the dialog. 187 * <p> 188 * This constructor sets the component's locale property to the value 189 * returned by {@code JComponent.getDefaultLocale}. 190 * <p> 191 * NOTE: This constructor does not allow you to create an unowned 192 * {@code JDialog}. To create an unowned {@code JDialog} 193 * you must use either the {@code JDialog(Window)} or 194 * {@code JDialog(Dialog)} constructor with an argument of 195 * {@code null}. 196 * 197 * @param owner the {@code Frame} from which the dialog is displayed 198 * @param modal specifies whether dialog blocks user input to other top-level 199 * windows when shown. If {@code true}, the modality type property is set to 200 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless. 201 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 202 * returns {@code true}. 203 * @see java.awt.GraphicsEnvironment#isHeadless 204 * @see JComponent#getDefaultLocale 205 */ 206 public JDialog(Frame owner, boolean modal) { 207 this(owner, "", modal); 208 } 209 210 /** 211 * Creates a modeless dialog with the specified title and 212 * with the specified owner frame. If {@code owner} 213 * is {@code null}, a shared, hidden frame will be set as the 214 * owner of the dialog. 215 * <p> 216 * This constructor sets the component's locale property to the value 217 * returned by {@code JComponent.getDefaultLocale}. 218 * <p> 219 * NOTE: This constructor does not allow you to create an unowned 220 * {@code JDialog}. To create an unowned {@code JDialog} 221 * you must use either the {@code JDialog(Window)} or 222 * {@code JDialog(Dialog)} constructor with an argument of 223 * {@code null}. 224 * 225 * @param owner the {@code Frame} from which the dialog is displayed 226 * @param title the {@code String} to display in the dialog's 227 * title bar 228 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 229 * returns {@code true}. 230 * @see java.awt.GraphicsEnvironment#isHeadless 231 * @see JComponent#getDefaultLocale 232 */ 233 public JDialog(Frame owner, String title) { 234 this(owner, title, false); 235 } 236 237 /** 238 * Creates a dialog with the specified title, owner {@code Frame} 239 * and modality. If {@code owner} is {@code null}, 240 * a shared, hidden frame will be set as the owner of this dialog. 241 * <p> 242 * This constructor sets the component's locale property to the value 243 * returned by {@code JComponent.getDefaultLocale}. 244 * <p> 245 * NOTE: Any popup components ({@code JComboBox}, 246 * {@code JPopupMenu}, {@code JMenuBar}) 247 * created within a modal dialog will be forced to be lightweight. 248 * <p> 249 * NOTE: This constructor does not allow you to create an unowned 250 * {@code JDialog}. To create an unowned {@code JDialog} 251 * you must use either the {@code JDialog(Window)} or 252 * {@code JDialog(Dialog)} constructor with an argument of 253 * {@code null}. 254 * 255 * @param owner the {@code Frame} from which the dialog is displayed 256 * @param title the {@code String} to display in the dialog's 257 * title bar 258 * @param modal specifies whether dialog blocks user input to other top-level 259 * windows when shown. If {@code true}, the modality type property is set to 260 * {@code DEFAULT_MODALITY_TYPE} otherwise the dialog is modeless 261 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 262 * returns {@code true}. 263 * 264 * @see java.awt.Dialog.ModalityType 265 * @see java.awt.Dialog.ModalityType#MODELESS 266 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 267 * @see java.awt.Dialog#setModal 268 * @see java.awt.Dialog#setModalityType 269 * @see java.awt.GraphicsEnvironment#isHeadless 270 * @see JComponent#getDefaultLocale 271 */ 272 public JDialog(Frame owner, String title, boolean modal) { 273 super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner, 274 title, modal); 275 if (owner == null) { 276 WindowListener ownerShutdownListener = 277 SwingUtilities.getSharedOwnerFrameShutdownListener(); 278 addWindowListener(ownerShutdownListener); 279 } 280 dialogInit(); 281 } 282 283 /** 284 * Creates a dialog with the specified title, 285 * owner {@code Frame}, modality and {@code GraphicsConfiguration}. 286 * If {@code owner} is {@code null}, 287 * a shared, hidden frame will be set as the owner of this dialog. 288 * <p> 289 * This constructor sets the component's locale property to the value 290 * returned by {@code JComponent.getDefaultLocale}. 291 * <p> 292 * NOTE: Any popup components ({@code JComboBox}, 293 * {@code JPopupMenu}, {@code JMenuBar}) 294 * created within a modal dialog will be forced to be lightweight. 295 * <p> 296 * NOTE: This constructor does not allow you to create an unowned 297 * {@code JDialog}. To create an unowned {@code JDialog} 298 * you must use either the {@code JDialog(Window)} or 299 * {@code JDialog(Dialog)} constructor with an argument of 300 * {@code null}. 301 * 302 * @param owner the {@code Frame} from which the dialog is displayed 303 * @param title the {@code String} to display in the dialog's 304 * title bar 305 * @param modal specifies whether dialog blocks user input to other top-level 306 * windows when shown. If {@code true}, the modality type property is set to 307 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless. 308 * @param gc the {@code GraphicsConfiguration} of the target screen device; 309 * if {@code null}, the default system {@code GraphicsConfiguration} 310 * is assumed 311 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 312 * returns {@code true}. 313 * @see java.awt.Dialog.ModalityType 314 * @see java.awt.Dialog.ModalityType#MODELESS 315 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 316 * @see java.awt.Dialog#setModal 317 * @see java.awt.Dialog#setModalityType 318 * @see java.awt.GraphicsEnvironment#isHeadless 319 * @see JComponent#getDefaultLocale 320 * @since 1.4 321 */ 322 public JDialog(Frame owner, String title, boolean modal, 323 GraphicsConfiguration gc) { 324 super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner, 325 title, modal, gc); 326 if (owner == null) { 327 WindowListener ownerShutdownListener = 328 SwingUtilities.getSharedOwnerFrameShutdownListener(); 329 addWindowListener(ownerShutdownListener); 330 } 331 dialogInit(); 332 } 333 334 /** 335 * Creates a modeless dialog with the specified {@code Dialog} 336 * as its owner and an empty title. 337 * <p> 338 * This constructor sets the component's locale property to the value 339 * returned by {@code JComponent.getDefaultLocale}. 340 * 341 * @param owner the owner {@code Dialog} from which the dialog is displayed 342 * or {@code null} if this dialog has no owner 343 * @throws HeadlessException {@code if GraphicsEnvironment.isHeadless()} 344 * returns {@code true}. 345 * @see java.awt.GraphicsEnvironment#isHeadless 346 * @see JComponent#getDefaultLocale 347 */ 348 public JDialog(Dialog owner) { 349 this(owner, false); 350 } 351 352 /** 353 * Creates a dialog with an empty title and the specified modality and 354 * {@code Dialog} as its owner. 355 * <p> 356 * This constructor sets the component's locale property to the value 357 * returned by {@code JComponent.getDefaultLocale}. 358 * 359 * @param owner the owner {@code Dialog} from which the dialog is displayed 360 * or {@code null} if this dialog has no owner 361 * @param modal specifies whether dialog blocks user input to other top-level 362 * windows when shown. If {@code true}, the modality type property is set to 363 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless. 364 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 365 * returns {@code true}. 366 * @see java.awt.Dialog.ModalityType 367 * @see java.awt.Dialog.ModalityType#MODELESS 368 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 369 * @see java.awt.Dialog#setModal 370 * @see java.awt.Dialog#setModalityType 371 * @see java.awt.GraphicsEnvironment#isHeadless 372 * @see JComponent#getDefaultLocale 373 */ 374 public JDialog(Dialog owner, boolean modal) { 375 this(owner, "", modal); 376 } 377 378 /** 379 * Creates a modeless dialog with the specified title and 380 * with the specified owner dialog. 381 * <p> 382 * This constructor sets the component's locale property to the value 383 * returned by {@code JComponent.getDefaultLocale}. 384 * 385 * @param owner the owner {@code Dialog} from which the dialog is displayed 386 * or {@code null} if this dialog has no owner 387 * @param title the {@code String} to display in the dialog's 388 * title bar 389 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 390 * returns {@code true}. 391 * @see java.awt.GraphicsEnvironment#isHeadless 392 * @see JComponent#getDefaultLocale 393 */ 394 public JDialog(Dialog owner, String title) { 395 this(owner, title, false); 396 } 397 398 /** 399 * Creates a dialog with the specified title, modality 400 * and the specified owner {@code Dialog}. 401 * <p> 402 * This constructor sets the component's locale property to the value 403 * returned by {@code JComponent.getDefaultLocale}. 404 * 405 * @param owner the owner {@code Dialog} from which the dialog is displayed 406 * or {@code null} if this dialog has no owner 407 * @param title the {@code String} to display in the dialog's 408 * title bar 409 * @param modal specifies whether dialog blocks user input to other top-level 410 * windows when shown. If {@code true}, the modality type property is set to 411 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless 412 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 413 * returns {@code true}. 414 * @see java.awt.Dialog.ModalityType 415 * @see java.awt.Dialog.ModalityType#MODELESS 416 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 417 * @see java.awt.Dialog#setModal 418 * @see java.awt.Dialog#setModalityType 419 * @see java.awt.GraphicsEnvironment#isHeadless 420 * @see JComponent#getDefaultLocale 421 */ 422 public JDialog(Dialog owner, String title, boolean modal) { 423 super(owner, title, modal); 424 dialogInit(); 425 } 426 427 /** 428 * Creates a dialog with the specified title, owner {@code Dialog}, 429 * modality and {@code GraphicsConfiguration}. 430 * 431 * <p> 432 * NOTE: Any popup components ({@code JComboBox}, 433 * {@code JPopupMenu}, {@code JMenuBar}) 434 * created within a modal dialog will be forced to be lightweight. 435 * <p> 436 * This constructor sets the component's locale property to the value 437 * returned by {@code JComponent.getDefaultLocale}. 438 * 439 * @param owner the owner {@code Dialog} from which the dialog is displayed 440 * or {@code null} if this dialog has no owner 441 * @param title the {@code String} to display in the dialog's 442 * title bar 443 * @param modal specifies whether dialog blocks user input to other top-level 444 * windows when shown. If {@code true}, the modality type property is set to 445 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless 446 * @param gc the {@code GraphicsConfiguration} of the target screen device; 447 * if {@code null}, the default system {@code GraphicsConfiguration} 448 * is assumed 449 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 450 * returns {@code true}. 451 * @see java.awt.Dialog.ModalityType 452 * @see java.awt.Dialog.ModalityType#MODELESS 453 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 454 * @see java.awt.Dialog#setModal 455 * @see java.awt.Dialog#setModalityType 456 * @see java.awt.GraphicsEnvironment#isHeadless 457 * @see JComponent#getDefaultLocale 458 * @since 1.4 459 */ 460 public JDialog(Dialog owner, String title, boolean modal, 461 GraphicsConfiguration gc) { 462 super(owner, title, modal, gc); 463 dialogInit(); 464 } 465 466 /** 467 * Creates a modeless dialog with the specified {@code Window} 468 * as its owner and an empty title. 469 * <p> 470 * This constructor sets the component's locale property to the value 471 * returned by {@code JComponent.getDefaultLocale}. 472 * 473 * @param owner the {@code Window} from which the dialog is displayed or 474 * {@code null} if this dialog has no owner 475 * 476 * @throws IllegalArgumentException 477 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 478 * or {@link java.awt.Frame Frame} 479 * @throws IllegalArgumentException 480 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 481 * @throws HeadlessException 482 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 483 * 484 * @see java.awt.GraphicsEnvironment#isHeadless 485 * @see JComponent#getDefaultLocale 486 * 487 * @since 1.6 488 */ 489 public JDialog(Window owner) { 490 this(owner, Dialog.ModalityType.MODELESS); 491 } 492 493 /** 494 * Creates a dialog with an empty title and the specified modality and 495 * {@code Window} as its owner. 496 * <p> 497 * This constructor sets the component's locale property to the value 498 * returned by {@code JComponent.getDefaultLocale}. 499 * 500 * @param owner the {@code Window} from which the dialog is displayed or 501 * {@code null} if this dialog has no owner 502 * @param modalityType specifies whether dialog blocks input to other 503 * windows when shown. {@code null} value and unsupported modality 504 * types are equivalent to {@code MODELESS} 505 * 506 * @throws IllegalArgumentException 507 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 508 * or {@link java.awt.Frame Frame} 509 * @throws IllegalArgumentException 510 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 511 * @throws HeadlessException 512 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 513 * @throws SecurityException 514 * if the calling thread does not have permission to create modal dialogs 515 * with the given {@code modalityType} 516 * 517 * @see java.awt.Dialog.ModalityType 518 * @see java.awt.Dialog#setModal 519 * @see java.awt.Dialog#setModalityType 520 * @see java.awt.GraphicsEnvironment#isHeadless 521 * @see JComponent#getDefaultLocale 522 * 523 * @since 1.6 524 */ 525 public JDialog(Window owner, ModalityType modalityType) { 526 this(owner, "", modalityType); 527 } 528 529 /** 530 * Creates a modeless dialog with the specified title and owner 531 * {@code Window}. 532 * <p> 533 * This constructor sets the component's locale property to the value 534 * returned by {@code JComponent.getDefaultLocale}. 535 * 536 * @param owner the {@code Window} from which the dialog is displayed or 537 * {@code null} if this dialog has no owner 538 * @param title the {@code String} to display in the dialog's 539 * title bar or {@code null} if the dialog has no title 540 * 541 * @throws IllegalArgumentException 542 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 543 * or {@link java.awt.Frame Frame} 544 * @throws IllegalArgumentException 545 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 546 * @throws HeadlessException 547 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 548 * 549 * @see java.awt.GraphicsEnvironment#isHeadless 550 * @see JComponent#getDefaultLocale 551 * 552 * @since 1.6 553 */ 554 public JDialog(Window owner, String title) { 555 this(owner, title, Dialog.ModalityType.MODELESS); 556 } 557 558 /** 559 * Creates a dialog with the specified title, owner {@code Window} and 560 * modality. 561 * <p> 562 * This constructor sets the component's locale property to the value 563 * returned by {@code JComponent.getDefaultLocale}. 564 * 565 * @param owner the {@code Window} from which the dialog is displayed or 566 * {@code null} if this dialog has no owner 567 * @param title the {@code String} to display in the dialog's 568 * title bar or {@code null} if the dialog has no title 569 * @param modalityType specifies whether dialog blocks input to other 570 * windows when shown. {@code null} value and unsupported modality 571 * types are equivalent to {@code MODELESS} 572 * 573 * @throws IllegalArgumentException 574 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 575 * or {@link java.awt.Frame Frame} 576 * @throws IllegalArgumentException 577 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 578 * @throws HeadlessException 579 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 580 * @throws SecurityException 581 * if the calling thread does not have permission to create modal dialogs 582 * with the given {@code modalityType} 583 * 584 * @see java.awt.Dialog.ModalityType 585 * @see java.awt.Dialog#setModal 586 * @see java.awt.Dialog#setModalityType 587 * @see java.awt.GraphicsEnvironment#isHeadless 588 * @see JComponent#getDefaultLocale 589 * 590 * @since 1.6 591 */ 592 public JDialog(Window owner, String title, Dialog.ModalityType modalityType) { 593 super(owner, title, modalityType); 594 dialogInit(); 595 } 596 597 /** 598 * Creates a dialog with the specified title, owner {@code Window}, 599 * modality and {@code GraphicsConfiguration}. 600 * <p> 601 * NOTE: Any popup components ({@code JComboBox}, 602 * {@code JPopupMenu}, {@code JMenuBar}) 603 * created within a modal dialog will be forced to be lightweight. 604 * <p> 605 * This constructor sets the component's locale property to the value 606 * returned by {@code JComponent.getDefaultLocale}. 607 * 608 * @param owner the {@code Window} from which the dialog is displayed or 609 * {@code null} if this dialog has no owner 610 * @param title the {@code String} to display in the dialog's 611 * title bar or {@code null} if the dialog has no title 612 * @param modalityType specifies whether dialog blocks input to other 613 * windows when shown. {@code null} value and unsupported modality 614 * types are equivalent to {@code MODELESS} 615 * @param gc the {@code GraphicsConfiguration} of the target screen device; 616 * if {@code null}, the default system {@code GraphicsConfiguration} 617 * is assumed 618 * @throws IllegalArgumentException 619 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 620 * or {@link java.awt.Frame Frame} 621 * @throws IllegalArgumentException 622 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 623 * @throws HeadlessException 624 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 625 * @throws SecurityException 626 * if the calling thread does not have permission to create modal dialogs 627 * with the given {@code modalityType} 628 * 629 * @see java.awt.Dialog.ModalityType 630 * @see java.awt.Dialog#setModal 631 * @see java.awt.Dialog#setModalityType 632 * @see java.awt.GraphicsEnvironment#isHeadless 633 * @see JComponent#getDefaultLocale 634 * 635 * @since 1.6 636 */ 637 public JDialog(Window owner, String title, Dialog.ModalityType modalityType, 638 GraphicsConfiguration gc) { 639 super(owner, title, modalityType, gc); 640 dialogInit(); 641 } 642 643 /** 644 * Called by the constructors to init the {@code JDialog} properly. 645 */ 646 protected void dialogInit() { 647 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK); 648 setLocale( JComponent.getDefaultLocale() ); 649 setRootPane(createRootPane()); 650 setRootPaneCheckingEnabled(true); 651 if (JDialog.isDefaultLookAndFeelDecorated()) { 652 boolean supportsWindowDecorations = 653 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 654 if (supportsWindowDecorations) { 655 setUndecorated(true); 656 getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); 657 } 658 } 659 sun.awt.SunToolkit.checkAndSetPolicy(this); 660 } 661 662 /** 663 * Called by the constructor methods to create the default 664 * {@code rootPane}. 665 */ 666 protected JRootPane createRootPane() { 667 JRootPane rp = new JRootPane(); 668 // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there 669 // is NO reason for the RootPane not to be opaque. For painting to 670 // work the contentPane must be opaque, therefor the RootPane can 671 // also be opaque. 672 rp.setOpaque(true); 673 return rp; 674 } 675 676 /** 677 * Handles window events depending on the state of the 678 * {@code defaultCloseOperation} property. 679 * 680 * @see #setDefaultCloseOperation 681 */ 682 protected void processWindowEvent(WindowEvent e) { 683 super.processWindowEvent(e); 684 685 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 686 switch(defaultCloseOperation) { 687 case HIDE_ON_CLOSE: 688 setVisible(false); 689 break; 690 case DISPOSE_ON_CLOSE: 691 dispose(); 692 break; 693 case DO_NOTHING_ON_CLOSE: 694 default: 695 break; 696 } 697 } 698 } 699 700 701 /** 702 * Sets the operation that will happen by default when 703 * the user initiates a "close" on this dialog. 704 * You must specify one of the following choices: 705 * <br><br> 706 * <ul> 707 * <li>{@code DO_NOTHING_ON_CLOSE} 708 * (defined in {@code WindowConstants}): 709 * Don't do anything; require the 710 * program to handle the operation in the {@code windowClosing} 711 * method of a registered {@code WindowListener} object. 712 * 713 * <li>{@code HIDE_ON_CLOSE} 714 * (defined in {@code WindowConstants}): 715 * Automatically hide the dialog after 716 * invoking any registered {@code WindowListener} 717 * objects. 718 * 719 * <li>{@code DISPOSE_ON_CLOSE} 720 * (defined in {@code WindowConstants}): 721 * Automatically hide and dispose the 722 * dialog after invoking any registered {@code WindowListener} 723 * objects. 724 * </ul> 725 * <p> 726 * The value is set to {@code HIDE_ON_CLOSE} by default. Changes 727 * to the value of this property cause the firing of a property 728 * change event, with property name "defaultCloseOperation". 729 * <p> 730 * <b>Note</b>: When the last displayable window within the 731 * Java virtual machine (VM) is disposed of, the VM may 732 * terminate. See <a href="../../java/awt/doc-files/AWTThreadIssues.html"> 733 * AWT Threading Issues</a> for more information. 734 * 735 * @param operation the operation which should be performed when the 736 * user closes the dialog 737 * @throws IllegalArgumentException if defaultCloseOperation value 738 * isn't one of the above valid values 739 * @see #addWindowListener 740 * @see #getDefaultCloseOperation 741 * @see WindowConstants 742 * 743 * @beaninfo 744 * preferred: true 745 * bound: true 746 * enum: DO_NOTHING_ON_CLOSE WindowConstants.DO_NOTHING_ON_CLOSE 747 * HIDE_ON_CLOSE WindowConstants.HIDE_ON_CLOSE 748 * DISPOSE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE 749 * description: The dialog's default close operation. 750 */ 751 public void setDefaultCloseOperation(int operation) { 752 if (operation != DO_NOTHING_ON_CLOSE && 753 operation != HIDE_ON_CLOSE && 754 operation != DISPOSE_ON_CLOSE) { 755 throw new IllegalArgumentException("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE"); 756 } 757 758 int oldValue = this.defaultCloseOperation; 759 this.defaultCloseOperation = operation; 760 firePropertyChange("defaultCloseOperation", oldValue, operation); 761 } 762 763 /** 764 * Returns the operation which occurs when the user 765 * initiates a "close" on this dialog. 766 * 767 * @return an integer indicating the window-close operation 768 * @see #setDefaultCloseOperation 769 */ 770 public int getDefaultCloseOperation() { 771 return defaultCloseOperation; 772 } 773 774 /** 775 * Sets the {@code transferHandler} property, which is a mechanism to 776 * support transfer of data into this component. Use {@code null} 777 * if the component does not support data transfer operations. 778 * <p> 779 * If the system property {@code suppressSwingDropSupport} is {@code false} 780 * (the default) and the current drop target on this component is either 781 * {@code null} or not a user-set drop target, this method will change the 782 * drop target as follows: If {@code newHandler} is {@code null} it will 783 * clear the drop target. If not {@code null} it will install a new 784 * {@code DropTarget}. 785 * <p> 786 * Note: When used with {@code JDialog}, {@code TransferHandler} only 787 * provides data import capability, as the data export related methods 788 * are currently typed to {@code JComponent}. 789 * <p> 790 * Please see 791 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html"> 792 * How to Use Drag and Drop and Data Transfer</a>, a section in 793 * <em>The Java Tutorial</em>, for more information. 794 * 795 * @param newHandler the new {@code TransferHandler} 796 * 797 * @see TransferHandler 798 * @see #getTransferHandler 799 * @see java.awt.Component#setDropTarget 800 * @since 1.6 801 * 802 * @beaninfo 803 * bound: true 804 * hidden: true 805 * description: Mechanism for transfer of data into the component 806 */ 807 public void setTransferHandler(TransferHandler newHandler) { 808 TransferHandler oldHandler = transferHandler; 809 transferHandler = newHandler; 810 SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler); 811 firePropertyChange("transferHandler", oldHandler, newHandler); 812 } 813 814 /** 815 * Gets the {@code transferHandler} property. 816 * 817 * @return the value of the {@code transferHandler} property 818 * 819 * @see TransferHandler 820 * @see #setTransferHandler 821 * @since 1.6 822 */ 823 public TransferHandler getTransferHandler() { 824 return transferHandler; 825 } 826 827 /** 828 * Calls {@code paint(g)}. This method was overridden to 829 * prevent an unnecessary call to clear the background. 830 * 831 * @param g the {@code Graphics} context in which to paint 832 */ 833 public void update(Graphics g) { 834 paint(g); 835 } 836 837 /** 838 * Sets the menubar for this dialog. 839 * 840 * @param menu the menubar being placed in the dialog 841 * 842 * @see #getJMenuBar 843 * 844 * @beaninfo 845 * hidden: true 846 * description: The menubar for accessing pulldown menus from this dialog. 847 */ 848 public void setJMenuBar(JMenuBar menu) { 849 getRootPane().setMenuBar(menu); 850 } 851 852 /** 853 * Returns the menubar set on this dialog. 854 * 855 * @see #setJMenuBar 856 */ 857 public JMenuBar getJMenuBar() { 858 return getRootPane().getMenuBar(); 859 } 860 861 862 /** 863 * Returns whether calls to {@code add} and 864 * {@code setLayout} are forwarded to the {@code contentPane}. 865 * 866 * @return true if {@code add} and {@code setLayout} 867 * are forwarded; false otherwise 868 * 869 * @see #addImpl 870 * @see #setLayout 871 * @see #setRootPaneCheckingEnabled 872 * @see javax.swing.RootPaneContainer 873 */ 874 protected boolean isRootPaneCheckingEnabled() { 875 return rootPaneCheckingEnabled; 876 } 877 878 879 /** 880 * Sets whether calls to {@code add} and 881 * {@code setLayout} are forwarded to the {@code contentPane}. 882 * 883 * @param enabled true if {@code add} and {@code setLayout} 884 * are forwarded, false if they should operate directly on the 885 * {@code JDialog}. 886 * 887 * @see #addImpl 888 * @see #setLayout 889 * @see #isRootPaneCheckingEnabled 890 * @see javax.swing.RootPaneContainer 891 * @beaninfo 892 * hidden: true 893 * description: Whether the add and setLayout methods are forwarded 894 */ 895 protected void setRootPaneCheckingEnabled(boolean enabled) { 896 rootPaneCheckingEnabled = enabled; 897 } 898 899 /** 900 * Adds the specified child {@code Component}. 901 * This method is overridden to conditionally forward calls to the 902 * {@code contentPane}. 903 * By default, children are added to the {@code contentPane} instead 904 * of the frame, refer to {@link javax.swing.RootPaneContainer} for 905 * details. 906 * 907 * @param comp the component to be enhanced 908 * @param constraints the constraints to be respected 909 * @param index the index 910 * @throws IllegalArgumentException if {@code index} is invalid 911 * @throws IllegalArgumentException if adding the container's parent 912 * to itself 913 * @throws IllegalArgumentException if adding a window to a container 914 * 915 * @see #setRootPaneCheckingEnabled 916 * @see javax.swing.RootPaneContainer 917 */ 918 protected void addImpl(Component comp, Object constraints, int index) 919 { 920 if(isRootPaneCheckingEnabled()) { 921 getContentPane().add(comp, constraints, index); 922 } 923 else { 924 super.addImpl(comp, constraints, index); 925 } 926 } 927 928 /** 929 * Removes the specified component from the container. If 930 * {@code comp} is not the {@code rootPane}, this will forward 931 * the call to the {@code contentPane}. This will do nothing if 932 * {@code comp} is not a child of the {@code JDialog} or 933 * {@code contentPane}. 934 * 935 * @param comp the component to be removed 936 * @throws NullPointerException if {@code comp} is null 937 * @see #add 938 * @see javax.swing.RootPaneContainer 939 */ 940 public void remove(Component comp) { 941 if (comp == rootPane) { 942 super.remove(comp); 943 } else { 944 getContentPane().remove(comp); 945 } 946 } 947 948 949 /** 950 * Sets the {@code LayoutManager}. 951 * Overridden to conditionally forward the call to the 952 * {@code contentPane}. 953 * Refer to {@link javax.swing.RootPaneContainer} for 954 * more information. 955 * 956 * @param manager the {@code LayoutManager} 957 * @see #setRootPaneCheckingEnabled 958 * @see javax.swing.RootPaneContainer 959 */ 960 public void setLayout(LayoutManager manager) { 961 if(isRootPaneCheckingEnabled()) { 962 getContentPane().setLayout(manager); 963 } 964 else { 965 super.setLayout(manager); 966 } 967 } 968 969 970 /** 971 * Returns the {@code rootPane} object for this dialog. 972 * 973 * @see #setRootPane 974 * @see RootPaneContainer#getRootPane 975 */ 976 public JRootPane getRootPane() { 977 return rootPane; 978 } 979 980 981 /** 982 * Sets the {@code rootPane} property. 983 * This method is called by the constructor. 984 * 985 * @param root the {@code rootPane} object for this dialog 986 * 987 * @see #getRootPane 988 * 989 * @beaninfo 990 * hidden: true 991 * description: the RootPane object for this dialog. 992 */ 993 protected void setRootPane(JRootPane root) { 994 if(rootPane != null) { 995 remove(rootPane); 996 } 997 rootPane = root; 998 if(rootPane != null) { 999 boolean checkingEnabled = isRootPaneCheckingEnabled(); 1000 try { 1001 setRootPaneCheckingEnabled(false); 1002 add(rootPane, BorderLayout.CENTER); 1003 } 1004 finally { 1005 setRootPaneCheckingEnabled(checkingEnabled); 1006 } 1007 } 1008 } 1009 1010 1011 /** 1012 * Returns the {@code contentPane} object for this dialog. 1013 * 1014 * @return the {@code contentPane} property 1015 * 1016 * @see #setContentPane 1017 * @see RootPaneContainer#getContentPane 1018 */ 1019 public Container getContentPane() { 1020 return getRootPane().getContentPane(); 1021 } 1022 1023 1024 /** 1025 * Sets the {@code contentPane} property. 1026 * This method is called by the constructor. 1027 * <p> 1028 * Swing's painting architecture requires an opaque {@code JComponent} 1029 * in the containment hierarchy. This is typically provided by the 1030 * content pane. If you replace the content pane it is recommended you 1031 * replace it with an opaque {@code JComponent}. 1032 * @see JRootPane 1033 * 1034 * @param contentPane the {@code contentPane} object for this dialog 1035 * 1036 * @throws java.awt.IllegalComponentStateException (a runtime 1037 * exception) if the content pane parameter is {@code null} 1038 * @see #getContentPane 1039 * @see RootPaneContainer#setContentPane 1040 * 1041 * @beaninfo 1042 * hidden: true 1043 * description: The client area of the dialog where child 1044 * components are normally inserted. 1045 */ 1046 public void setContentPane(Container contentPane) { 1047 getRootPane().setContentPane(contentPane); 1048 } 1049 1050 /** 1051 * Returns the {@code layeredPane} object for this dialog. 1052 * 1053 * @return the {@code layeredPane} property 1054 * 1055 * @see #setLayeredPane 1056 * @see RootPaneContainer#getLayeredPane 1057 */ 1058 public JLayeredPane getLayeredPane() { 1059 return getRootPane().getLayeredPane(); 1060 } 1061 1062 /** 1063 * Sets the {@code layeredPane} property. 1064 * This method is called by the constructor. 1065 * 1066 * @param layeredPane the new {@code layeredPane} property 1067 * 1068 * @throws java.awt.IllegalComponentStateException (a runtime 1069 * exception) if the layered pane parameter is null 1070 * @see #getLayeredPane 1071 * @see RootPaneContainer#setLayeredPane 1072 * 1073 * @beaninfo 1074 * hidden: true 1075 * description: The pane which holds the various dialog layers. 1076 */ 1077 public void setLayeredPane(JLayeredPane layeredPane) { 1078 getRootPane().setLayeredPane(layeredPane); 1079 } 1080 1081 /** 1082 * Returns the {@code glassPane} object for this dialog. 1083 * 1084 * @return the {@code glassPane} property 1085 * 1086 * @see #setGlassPane 1087 * @see RootPaneContainer#getGlassPane 1088 */ 1089 public Component getGlassPane() { 1090 return getRootPane().getGlassPane(); 1091 } 1092 1093 /** 1094 * Sets the {@code glassPane} property. 1095 * This method is called by the constructor. 1096 * 1097 * @param glassPane the {@code glassPane} object for this dialog 1098 * @see #getGlassPane 1099 * @see RootPaneContainer#setGlassPane 1100 * 1101 * @beaninfo 1102 * hidden: true 1103 * description: A transparent pane used for menu rendering. 1104 */ 1105 public void setGlassPane(Component glassPane) { 1106 getRootPane().setGlassPane(glassPane); 1107 } 1108 1109 /** 1110 * {@inheritDoc} 1111 * 1112 * @since 1.6 1113 */ 1114 public Graphics getGraphics() { 1115 JComponent.getGraphicsInvoked(this); 1116 return super.getGraphics(); 1117 } 1118 1119 /** 1120 * Repaints the specified rectangle of this component within 1121 * {@code time} milliseconds. Refer to {@code RepaintManager} 1122 * for details on how the repaint is handled. 1123 * 1124 * @param time maximum time in milliseconds before update 1125 * @param x the <i>x</i> coordinate 1126 * @param y the <i>y</i> coordinate 1127 * @param width the width 1128 * @param height the height 1129 * @see RepaintManager 1130 * @since 1.6 1131 */ 1132 public void repaint(long time, int x, int y, int width, int height) { 1133 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) { 1134 RepaintManager.currentManager(this).addDirtyRegion( 1135 this, x, y, width, height); 1136 } 1137 else { 1138 super.repaint(time, x, y, width, height); 1139 } 1140 } 1141 1142 /** 1143 * Provides a hint as to whether or not newly created {@code JDialog}s 1144 * should have their Window decorations (such as borders, widgets to 1145 * close the window, title...) provided by the current look 1146 * and feel. If {@code defaultLookAndFeelDecorated} is true, 1147 * the current {@code LookAndFeel} supports providing window 1148 * decorations, and the current window manager supports undecorated 1149 * windows, then newly created {@code JDialog}s will have their 1150 * Window decorations provided by the current {@code LookAndFeel}. 1151 * Otherwise, newly created {@code JDialog}s will have their 1152 * Window decorations provided by the current window manager. 1153 * <p> 1154 * You can get the same effect on a single JDialog by doing the following: 1155 * <pre> 1156 * JDialog dialog = new JDialog(); 1157 * dialog.setUndecorated(true); 1158 * dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); 1159 * </pre> 1160 * 1161 * @param defaultLookAndFeelDecorated A hint as to whether or not current 1162 * look and feel should provide window decorations 1163 * @see javax.swing.LookAndFeel#getSupportsWindowDecorations 1164 * @since 1.4 1165 */ 1166 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) { 1167 if (defaultLookAndFeelDecorated) { 1168 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE); 1169 } else { 1170 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE); 1171 } 1172 } 1173 1174 /** 1175 * Returns true if newly created {@code JDialog}s should have their 1176 * Window decorations provided by the current look and feel. This is only 1177 * a hint, as certain look and feels may not support this feature. 1178 * 1179 * @return true if look and feel should provide Window decorations. 1180 * @since 1.4 1181 */ 1182 public static boolean isDefaultLookAndFeelDecorated() { 1183 Boolean defaultLookAndFeelDecorated = 1184 (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey); 1185 if (defaultLookAndFeelDecorated == null) { 1186 defaultLookAndFeelDecorated = Boolean.FALSE; 1187 } 1188 return defaultLookAndFeelDecorated.booleanValue(); 1189 } 1190 1191 /** 1192 * Returns a string representation of this {@code JDialog}. 1193 * This method 1194 * is intended to be used only for debugging purposes, and the 1195 * content and format of the returned string may vary between 1196 * implementations. The returned string may be empty but may not 1197 * be {@code null}. 1198 * 1199 * @return a string representation of this {@code JDialog}. 1200 */ 1201 protected String paramString() { 1202 String defaultCloseOperationString; 1203 if (defaultCloseOperation == HIDE_ON_CLOSE) { 1204 defaultCloseOperationString = "HIDE_ON_CLOSE"; 1205 } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) { 1206 defaultCloseOperationString = "DISPOSE_ON_CLOSE"; 1207 } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) { 1208 defaultCloseOperationString = "DO_NOTHING_ON_CLOSE"; 1209 } else defaultCloseOperationString = ""; 1210 String rootPaneString = (rootPane != null ? 1211 rootPane.toString() : ""); 1212 String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ? 1213 "true" : "false"); 1214 1215 return super.paramString() + 1216 ",defaultCloseOperation=" + defaultCloseOperationString + 1217 ",rootPane=" + rootPaneString + 1218 ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString; 1219 } 1220 1221 1222 ///////////////// 1223 // Accessibility support 1224 //////////////// 1225 1226 protected AccessibleContext accessibleContext = null; 1227 1228 /** 1229 * Gets the AccessibleContext associated with this JDialog. 1230 * For JDialogs, the AccessibleContext takes the form of an 1231 * AccessibleJDialog. 1232 * A new AccessibleJDialog instance is created if necessary. 1233 * 1234 * @return an AccessibleJDialog that serves as the 1235 * AccessibleContext of this JDialog 1236 */ 1237 public AccessibleContext getAccessibleContext() { 1238 if (accessibleContext == null) { 1239 accessibleContext = new AccessibleJDialog(); 1240 } 1241 return accessibleContext; 1242 } 1243 1244 /** 1245 * This class implements accessibility support for the 1246 * {@code JDialog} class. It provides an implementation of the 1247 * Java Accessibility API appropriate to dialog user-interface 1248 * elements. 1249 */ 1250 protected class AccessibleJDialog extends AccessibleAWTDialog { 1251 1252 // AccessibleContext methods 1253 // 1254 /** 1255 * Get the accessible name of this object. 1256 * 1257 * @return the localized name of the object -- can be null if this 1258 * object does not have a name 1259 */ 1260 public String getAccessibleName() { 1261 if (accessibleName != null) { 1262 return accessibleName; 1263 } else { 1264 if (getTitle() == null) { 1265 return super.getAccessibleName(); 1266 } else { 1267 return getTitle(); 1268 } 1269 } 1270 } 1271 1272 /** 1273 * Get the state of this object. 1274 * 1275 * @return an instance of AccessibleStateSet containing the current 1276 * state set of the object 1277 * @see AccessibleState 1278 */ 1279 public AccessibleStateSet getAccessibleStateSet() { 1280 AccessibleStateSet states = super.getAccessibleStateSet(); 1281 1282 if (isResizable()) { 1283 states.add(AccessibleState.RESIZABLE); 1284 } 1285 if (getFocusOwner() != null) { 1286 states.add(AccessibleState.ACTIVE); 1287 } 1288 if (isModal()) { 1289 states.add(AccessibleState.MODAL); 1290 } 1291 return states; 1292 } 1293 1294 } // inner class AccessibleJDialog 1295 }