1 /* 2 * Copyright (c) 1997, 2013, 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 public class JDialog extends Dialog implements WindowConstants, 99 Accessible, 100 RootPaneContainer, 101 TransferHandler.HasGetTransferHandler 102 { 103 /** 104 * Key into the AppContext, used to check if should provide decorations 105 * by default. 106 */ 107 private static final Object defaultLookAndFeelDecoratedKey = 108 new StringBuffer("JDialog.defaultLookAndFeelDecorated"); 109 110 private int defaultCloseOperation = HIDE_ON_CLOSE; 111 112 /** 113 * @see #getRootPane 114 * @see #setRootPane 115 */ 116 protected JRootPane rootPane; 117 118 /** 119 * If true then calls to {@code add} and {@code setLayout} 120 * will be forwarded to the {@code contentPane}. This is initially 121 * false, but is set to true when the {@code JDialog} is constructed. 122 * 123 * @see #isRootPaneCheckingEnabled 124 * @see #setRootPaneCheckingEnabled 125 * @see javax.swing.RootPaneContainer 126 */ 127 protected boolean rootPaneCheckingEnabled = false; 128 129 /** 130 * The {@code TransferHandler} for this dialog. 131 */ 132 private TransferHandler transferHandler; 133 134 /** 135 * Creates a modeless dialog without a title and without a specified 136 * {@code Frame} owner. A shared, hidden frame will be 137 * set as the owner of the dialog. 138 * <p> 139 * This constructor sets the component's locale property to the value 140 * returned by {@code JComponent.getDefaultLocale}. 141 * <p> 142 * NOTE: This constructor does not allow you to create an unowned 143 * {@code JDialog}. To create an unowned {@code JDialog} 144 * you must use either the {@code JDialog(Window)} or 145 * {@code JDialog(Dialog)} constructor with an argument of 146 * {@code null}. 147 * 148 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 149 * returns {@code true}. 150 * @see java.awt.GraphicsEnvironment#isHeadless 151 * @see JComponent#getDefaultLocale 152 */ 153 public JDialog() { 154 this((Frame)null, false); 155 } 156 157 /** 158 * Creates a modeless dialog with the specified {@code Frame} 159 * as its owner and an empty title. If {@code owner} 160 * is {@code null}, a shared, hidden frame will be set as the 161 * owner of the dialog. 162 * <p> 163 * This constructor sets the component's locale property to the value 164 * returned by {@code JComponent.getDefaultLocale}. 165 * <p> 166 * NOTE: This constructor does not allow you to create an unowned 167 * {@code JDialog}. To create an unowned {@code JDialog} 168 * you must use either the {@code JDialog(Window)} or 169 * {@code JDialog(Dialog)} constructor with an argument of 170 * {@code null}. 171 * 172 * @param owner the {@code Frame} from which the dialog is displayed 173 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 174 * returns {@code true}. 175 * @see java.awt.GraphicsEnvironment#isHeadless 176 * @see JComponent#getDefaultLocale 177 */ 178 public JDialog(Frame owner) { 179 this(owner, false); 180 } 181 182 /** 183 * Creates a dialog with an empty title and the specified modality and 184 * {@code Frame} as its owner. If {@code owner} is {@code null}, 185 * a shared, hidden frame will be set as the owner of the dialog. 186 * <p> 187 * This constructor sets the component's locale property to the value 188 * returned by {@code JComponent.getDefaultLocale}. 189 * <p> 190 * NOTE: This constructor does not allow you to create an unowned 191 * {@code JDialog}. To create an unowned {@code JDialog} 192 * you must use either the {@code JDialog(Window)} or 193 * {@code JDialog(Dialog)} constructor with an argument of 194 * {@code null}. 195 * 196 * @param owner the {@code Frame} from which the dialog is displayed 197 * @param modal specifies whether dialog blocks user input to other top-level 198 * windows when shown. If {@code true}, the modality type property is set to 199 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless. 200 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 201 * returns {@code true}. 202 * @see java.awt.GraphicsEnvironment#isHeadless 203 * @see JComponent#getDefaultLocale 204 */ 205 public JDialog(Frame owner, boolean modal) { 206 this(owner, "", modal); 207 } 208 209 /** 210 * Creates a modeless dialog with the specified title and 211 * with the specified owner frame. If {@code owner} 212 * is {@code null}, a shared, hidden frame will be set as the 213 * owner of the dialog. 214 * <p> 215 * This constructor sets the component's locale property to the value 216 * returned by {@code JComponent.getDefaultLocale}. 217 * <p> 218 * NOTE: This constructor does not allow you to create an unowned 219 * {@code JDialog}. To create an unowned {@code JDialog} 220 * you must use either the {@code JDialog(Window)} or 221 * {@code JDialog(Dialog)} constructor with an argument of 222 * {@code null}. 223 * 224 * @param owner the {@code Frame} from which the dialog is displayed 225 * @param title the {@code String} to display in the dialog's 226 * title bar 227 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 228 * returns {@code true}. 229 * @see java.awt.GraphicsEnvironment#isHeadless 230 * @see JComponent#getDefaultLocale 231 */ 232 public JDialog(Frame owner, String title) { 233 this(owner, title, false); 234 } 235 236 /** 237 * Creates a dialog with the specified title, owner {@code Frame} 238 * and modality. If {@code owner} is {@code null}, 239 * a shared, hidden frame will be set as the owner of this dialog. 240 * <p> 241 * This constructor sets the component's locale property to the value 242 * returned by {@code JComponent.getDefaultLocale}. 243 * <p> 244 * NOTE: Any popup components ({@code JComboBox}, 245 * {@code JPopupMenu}, {@code JMenuBar}) 246 * created within a modal dialog will be forced to be lightweight. 247 * <p> 248 * NOTE: This constructor does not allow you to create an unowned 249 * {@code JDialog}. To create an unowned {@code JDialog} 250 * you must use either the {@code JDialog(Window)} or 251 * {@code JDialog(Dialog)} constructor with an argument of 252 * {@code null}. 253 * 254 * @param owner the {@code Frame} from which the dialog is displayed 255 * @param title the {@code String} to display in the dialog's 256 * title bar 257 * @param modal specifies whether dialog blocks user input to other top-level 258 * windows when shown. If {@code true}, the modality type property is set to 259 * {@code DEFAULT_MODALITY_TYPE} otherwise the dialog is modeless 260 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 261 * returns {@code true}. 262 * 263 * @see java.awt.Dialog.ModalityType 264 * @see java.awt.Dialog.ModalityType#MODELESS 265 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 266 * @see java.awt.Dialog#setModal 267 * @see java.awt.Dialog#setModalityType 268 * @see java.awt.GraphicsEnvironment#isHeadless 269 * @see JComponent#getDefaultLocale 270 */ 271 public JDialog(Frame owner, String title, boolean modal) { 272 super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner, 273 title, modal); 274 if (owner == null) { 275 WindowListener ownerShutdownListener = 276 SwingUtilities.getSharedOwnerFrameShutdownListener(); 277 addWindowListener(ownerShutdownListener); 278 } 279 dialogInit(); 280 } 281 282 /** 283 * Creates a dialog with the specified title, 284 * owner {@code Frame}, modality and {@code GraphicsConfiguration}. 285 * If {@code owner} is {@code null}, 286 * a shared, hidden frame will be set as the owner of this dialog. 287 * <p> 288 * This constructor sets the component's locale property to the value 289 * returned by {@code JComponent.getDefaultLocale}. 290 * <p> 291 * NOTE: Any popup components ({@code JComboBox}, 292 * {@code JPopupMenu}, {@code JMenuBar}) 293 * created within a modal dialog will be forced to be lightweight. 294 * <p> 295 * NOTE: This constructor does not allow you to create an unowned 296 * {@code JDialog}. To create an unowned {@code JDialog} 297 * you must use either the {@code JDialog(Window)} or 298 * {@code JDialog(Dialog)} constructor with an argument of 299 * {@code null}. 300 * 301 * @param owner the {@code Frame} from which the dialog is displayed 302 * @param title the {@code String} to display in the dialog's 303 * title bar 304 * @param modal specifies whether dialog blocks user input to other top-level 305 * windows when shown. If {@code true}, the modality type property is set to 306 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless. 307 * @param gc the {@code GraphicsConfiguration} of the target screen device; 308 * if {@code null}, the default system {@code GraphicsConfiguration} 309 * is assumed 310 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 311 * returns {@code true}. 312 * @see java.awt.Dialog.ModalityType 313 * @see java.awt.Dialog.ModalityType#MODELESS 314 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 315 * @see java.awt.Dialog#setModal 316 * @see java.awt.Dialog#setModalityType 317 * @see java.awt.GraphicsEnvironment#isHeadless 318 * @see JComponent#getDefaultLocale 319 * @since 1.4 320 */ 321 public JDialog(Frame owner, String title, boolean modal, 322 GraphicsConfiguration gc) { 323 super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner, 324 title, modal, gc); 325 if (owner == null) { 326 WindowListener ownerShutdownListener = 327 SwingUtilities.getSharedOwnerFrameShutdownListener(); 328 addWindowListener(ownerShutdownListener); 329 } 330 dialogInit(); 331 } 332 333 /** 334 * Creates a modeless dialog with the specified {@code Dialog} 335 * as its owner and an empty title. 336 * <p> 337 * This constructor sets the component's locale property to the value 338 * returned by {@code JComponent.getDefaultLocale}. 339 * 340 * @param owner the owner {@code Dialog} from which the dialog is displayed 341 * or {@code null} if this dialog has no owner 342 * @throws HeadlessException {@code if GraphicsEnvironment.isHeadless()} 343 * returns {@code true}. 344 * @see java.awt.GraphicsEnvironment#isHeadless 345 * @see JComponent#getDefaultLocale 346 */ 347 public JDialog(Dialog owner) { 348 this(owner, false); 349 } 350 351 /** 352 * Creates a dialog with an empty title and the specified modality and 353 * {@code Dialog} as its owner. 354 * <p> 355 * This constructor sets the component's locale property to the value 356 * returned by {@code JComponent.getDefaultLocale}. 357 * 358 * @param owner the owner {@code Dialog} from which the dialog is displayed 359 * or {@code null} if this dialog has no owner 360 * @param modal specifies whether dialog blocks user input to other top-level 361 * windows when shown. If {@code true}, the modality type property is set to 362 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless. 363 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 364 * returns {@code true}. 365 * @see java.awt.Dialog.ModalityType 366 * @see java.awt.Dialog.ModalityType#MODELESS 367 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 368 * @see java.awt.Dialog#setModal 369 * @see java.awt.Dialog#setModalityType 370 * @see java.awt.GraphicsEnvironment#isHeadless 371 * @see JComponent#getDefaultLocale 372 */ 373 public JDialog(Dialog owner, boolean modal) { 374 this(owner, "", modal); 375 } 376 377 /** 378 * Creates a modeless dialog with the specified title and 379 * with the specified owner dialog. 380 * <p> 381 * This constructor sets the component's locale property to the value 382 * returned by {@code JComponent.getDefaultLocale}. 383 * 384 * @param owner the owner {@code Dialog} from which the dialog is displayed 385 * or {@code null} if this dialog has no owner 386 * @param title the {@code String} to display in the dialog's 387 * title bar 388 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 389 * returns {@code true}. 390 * @see java.awt.GraphicsEnvironment#isHeadless 391 * @see JComponent#getDefaultLocale 392 */ 393 public JDialog(Dialog owner, String title) { 394 this(owner, title, false); 395 } 396 397 /** 398 * Creates a dialog with the specified title, modality 399 * and the specified owner {@code Dialog}. 400 * <p> 401 * This constructor sets the component's locale property to the value 402 * returned by {@code JComponent.getDefaultLocale}. 403 * 404 * @param owner the owner {@code Dialog} from which the dialog is displayed 405 * or {@code null} if this dialog has no owner 406 * @param title the {@code String} to display in the dialog's 407 * title bar 408 * @param modal specifies whether dialog blocks user input to other top-level 409 * windows when shown. If {@code true}, the modality type property is set to 410 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless 411 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 412 * returns {@code true}. 413 * @see java.awt.Dialog.ModalityType 414 * @see java.awt.Dialog.ModalityType#MODELESS 415 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 416 * @see java.awt.Dialog#setModal 417 * @see java.awt.Dialog#setModalityType 418 * @see java.awt.GraphicsEnvironment#isHeadless 419 * @see JComponent#getDefaultLocale 420 */ 421 public JDialog(Dialog owner, String title, boolean modal) { 422 super(owner, title, modal); 423 dialogInit(); 424 } 425 426 /** 427 * Creates a dialog with the specified title, owner {@code Dialog}, 428 * modality and {@code GraphicsConfiguration}. 429 * 430 * <p> 431 * NOTE: Any popup components ({@code JComboBox}, 432 * {@code JPopupMenu}, {@code JMenuBar}) 433 * created within a modal dialog will be forced to be lightweight. 434 * <p> 435 * This constructor sets the component's locale property to the value 436 * returned by {@code JComponent.getDefaultLocale}. 437 * 438 * @param owner the owner {@code Dialog} from which the dialog is displayed 439 * or {@code null} if this dialog has no owner 440 * @param title the {@code String} to display in the dialog's 441 * title bar 442 * @param modal specifies whether dialog blocks user input to other top-level 443 * windows when shown. If {@code true}, the modality type property is set to 444 * {@code DEFAULT_MODALITY_TYPE}, otherwise the dialog is modeless 445 * @param gc the {@code GraphicsConfiguration} of the target screen device; 446 * if {@code null}, the default system {@code GraphicsConfiguration} 447 * is assumed 448 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()} 449 * returns {@code true}. 450 * @see java.awt.Dialog.ModalityType 451 * @see java.awt.Dialog.ModalityType#MODELESS 452 * @see java.awt.Dialog#DEFAULT_MODALITY_TYPE 453 * @see java.awt.Dialog#setModal 454 * @see java.awt.Dialog#setModalityType 455 * @see java.awt.GraphicsEnvironment#isHeadless 456 * @see JComponent#getDefaultLocale 457 * @since 1.4 458 */ 459 public JDialog(Dialog owner, String title, boolean modal, 460 GraphicsConfiguration gc) { 461 super(owner, title, modal, gc); 462 dialogInit(); 463 } 464 465 /** 466 * Creates a modeless dialog with the specified {@code Window} 467 * as its owner and an empty title. 468 * <p> 469 * This constructor sets the component's locale property to the value 470 * returned by {@code JComponent.getDefaultLocale}. 471 * 472 * @param owner the {@code Window} from which the dialog is displayed or 473 * {@code null} if this dialog has no owner 474 * 475 * @throws IllegalArgumentException 476 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 477 * or {@link java.awt.Frame Frame} 478 * @throws IllegalArgumentException 479 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 480 * @throws HeadlessException 481 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 482 * 483 * @see java.awt.GraphicsEnvironment#isHeadless 484 * @see JComponent#getDefaultLocale 485 * 486 * @since 1.6 487 */ 488 public JDialog(Window owner) { 489 this(owner, Dialog.ModalityType.MODELESS); 490 } 491 492 /** 493 * Creates a dialog with an empty title and the specified modality and 494 * {@code Window} as its owner. 495 * <p> 496 * This constructor sets the component's locale property to the value 497 * returned by {@code JComponent.getDefaultLocale}. 498 * 499 * @param owner the {@code Window} from which the dialog is displayed or 500 * {@code null} if this dialog has no owner 501 * @param modalityType specifies whether dialog blocks input to other 502 * windows when shown. {@code null} value and unsupported modality 503 * types are equivalent to {@code MODELESS} 504 * 505 * @throws IllegalArgumentException 506 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 507 * or {@link java.awt.Frame Frame} 508 * @throws IllegalArgumentException 509 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 510 * @throws HeadlessException 511 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 512 * @throws SecurityException 513 * if the calling thread does not have permission to create modal dialogs 514 * with the given {@code modalityType} 515 * 516 * @see java.awt.Dialog.ModalityType 517 * @see java.awt.Dialog#setModal 518 * @see java.awt.Dialog#setModalityType 519 * @see java.awt.GraphicsEnvironment#isHeadless 520 * @see JComponent#getDefaultLocale 521 * 522 * @since 1.6 523 */ 524 public JDialog(Window owner, ModalityType modalityType) { 525 this(owner, "", modalityType); 526 } 527 528 /** 529 * Creates a modeless dialog with the specified title and owner 530 * {@code Window}. 531 * <p> 532 * This constructor sets the component's locale property to the value 533 * returned by {@code JComponent.getDefaultLocale}. 534 * 535 * @param owner the {@code Window} from which the dialog is displayed or 536 * {@code null} if this dialog has no owner 537 * @param title the {@code String} to display in the dialog's 538 * title bar or {@code null} if the dialog has no title 539 * 540 * @throws IllegalArgumentException 541 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 542 * or {@link java.awt.Frame Frame} 543 * @throws IllegalArgumentException 544 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 545 * @throws HeadlessException 546 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 547 * 548 * @see java.awt.GraphicsEnvironment#isHeadless 549 * @see JComponent#getDefaultLocale 550 * 551 * @since 1.6 552 */ 553 public JDialog(Window owner, String title) { 554 this(owner, title, Dialog.ModalityType.MODELESS); 555 } 556 557 /** 558 * Creates a dialog with the specified title, owner {@code Window} and 559 * modality. 560 * <p> 561 * This constructor sets the component's locale property to the value 562 * returned by {@code JComponent.getDefaultLocale}. 563 * 564 * @param owner the {@code Window} from which the dialog is displayed or 565 * {@code null} if this dialog has no owner 566 * @param title the {@code String} to display in the dialog's 567 * title bar or {@code null} if the dialog has no title 568 * @param modalityType specifies whether dialog blocks input to other 569 * windows when shown. {@code null} value and unsupported modality 570 * types are equivalent to {@code MODELESS} 571 * 572 * @throws IllegalArgumentException 573 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 574 * or {@link java.awt.Frame Frame} 575 * @throws IllegalArgumentException 576 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 577 * @throws HeadlessException 578 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 579 * @throws SecurityException 580 * if the calling thread does not have permission to create modal dialogs 581 * with the given {@code modalityType} 582 * 583 * @see java.awt.Dialog.ModalityType 584 * @see java.awt.Dialog#setModal 585 * @see java.awt.Dialog#setModalityType 586 * @see java.awt.GraphicsEnvironment#isHeadless 587 * @see JComponent#getDefaultLocale 588 * 589 * @since 1.6 590 */ 591 public JDialog(Window owner, String title, Dialog.ModalityType modalityType) { 592 super(owner, title, modalityType); 593 dialogInit(); 594 } 595 596 /** 597 * Creates a dialog with the specified title, owner {@code Window}, 598 * modality and {@code GraphicsConfiguration}. 599 * <p> 600 * NOTE: Any popup components ({@code JComboBox}, 601 * {@code JPopupMenu}, {@code JMenuBar}) 602 * created within a modal dialog will be forced to be lightweight. 603 * <p> 604 * This constructor sets the component's locale property to the value 605 * returned by {@code JComponent.getDefaultLocale}. 606 * 607 * @param owner the {@code Window} from which the dialog is displayed or 608 * {@code null} if this dialog has no owner 609 * @param title the {@code String} to display in the dialog's 610 * title bar or {@code null} if the dialog has no title 611 * @param modalityType specifies whether dialog blocks input to other 612 * windows when shown. {@code null} value and unsupported modality 613 * types are equivalent to {@code MODELESS} 614 * @param gc the {@code GraphicsConfiguration} of the target screen device; 615 * if {@code null}, the default system {@code GraphicsConfiguration} 616 * is assumed 617 * @throws IllegalArgumentException 618 * if the {@code owner} is not an instance of {@link java.awt.Dialog Dialog} 619 * or {@link java.awt.Frame Frame} 620 * @throws IllegalArgumentException 621 * if the {@code owner}'s {@code GraphicsConfiguration} is not from a screen device 622 * @throws HeadlessException 623 * when {@code GraphicsEnvironment.isHeadless()} returns {@code true} 624 * @throws SecurityException 625 * if the calling thread does not have permission to create modal dialogs 626 * with the given {@code modalityType} 627 * 628 * @see java.awt.Dialog.ModalityType 629 * @see java.awt.Dialog#setModal 630 * @see java.awt.Dialog#setModalityType 631 * @see java.awt.GraphicsEnvironment#isHeadless 632 * @see JComponent#getDefaultLocale 633 * 634 * @since 1.6 635 */ 636 public JDialog(Window owner, String title, Dialog.ModalityType modalityType, 637 GraphicsConfiguration gc) { 638 super(owner, title, modalityType, gc); 639 dialogInit(); 640 } 641 642 /** 643 * Called by the constructors to init the {@code JDialog} properly. 644 */ 645 protected void dialogInit() { 646 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK); 647 setLocale( JComponent.getDefaultLocale() ); 648 setRootPane(createRootPane()); 649 setBackground(UIManager.getColor("control")); 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 }