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.AWTEvent; 28 import java.awt.BorderLayout; 29 import java.awt.Component; 30 import java.awt.Container; 31 import java.awt.Frame; 32 import java.awt.Graphics; 33 import java.awt.GraphicsConfiguration; 34 import java.awt.HeadlessException; 35 import java.awt.Image; 36 import java.awt.LayoutManager; 37 import java.awt.event.WindowEvent; 38 39 import javax.accessibility.Accessible; 40 import javax.accessibility.AccessibleContext; 41 import javax.accessibility.AccessibleState; 42 import javax.accessibility.AccessibleStateSet; 43 44 45 /** 46 * An extended version of <code>java.awt.Frame</code> that adds support for 47 * the JFC/Swing component architecture. 48 * You can find task-oriented documentation about using <code>JFrame</code> 49 * in <em>The Java Tutorial</em>, in the section 50 * <a 51 href="https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html">How to Make Frames</a>. 52 * 53 * <p> 54 * The <code>JFrame</code> class is slightly incompatible with <code>Frame</code>. 55 * Like all other JFC/Swing top-level containers, 56 * a <code>JFrame</code> contains a <code>JRootPane</code> as its only child. 57 * The <b>content pane</b> provided by the root pane should, 58 * as a rule, contain 59 * all the non-menu components displayed by the <code>JFrame</code>. 60 * This is different from the AWT <code>Frame</code> case. 61 * As a convenience, the {@code add}, {@code remove}, and {@code setLayout} 62 * methods of this class are overridden, so that they delegate calls 63 * to the corresponding methods of the {@code ContentPane}. 64 * For example, you can add a child component to a frame as follows: 65 * <pre> 66 * frame.add(child); 67 * </pre> 68 * And the child will be added to the contentPane. 69 * The content pane will 70 * always be non-null. Attempting to set it to null will cause the JFrame 71 * to throw an exception. The default content pane will have a BorderLayout 72 * manager set on it. 73 * Refer to {@link javax.swing.RootPaneContainer} 74 * for details on adding, removing and setting the <code>LayoutManager</code> 75 * of a <code>JFrame</code>. 76 * <p> 77 * Unlike a <code>Frame</code>, a <code>JFrame</code> has some notion of how to 78 * respond when the user attempts to close the window. The default behavior 79 * is to simply hide the JFrame when the user closes the window. To change the 80 * default behavior, you invoke the method 81 * {@link #setDefaultCloseOperation}. 82 * To make the <code>JFrame</code> behave the same as a <code>Frame</code> 83 * instance, use 84 * <code>setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)</code>. 85 * <p> 86 * For more information on content panes 87 * and other features that root panes provide, 88 * see <a 89 href="https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html">Using Top-Level Containers</a> in <em>The Java Tutorial</em>. 90 * <p> 91 * In a multi-screen environment, you can create a <code>JFrame</code> 92 * on a different screen device. See {@link java.awt.Frame} for more 93 * information. 94 * <p> 95 * <strong>Warning:</strong> Swing is not thread safe. For more 96 * information see <a 97 * href="package-summary.html#threading">Swing's Threading 98 * Policy</a>. 99 * <p> 100 * <strong>Warning:</strong> 101 * Serialized objects of this class will not be compatible with 102 * future Swing releases. The current serialization support is 103 * appropriate for short term storage or RMI between applications running 104 * the same version of Swing. As of 1.4, support for long term storage 105 * of all JavaBeans™ 106 * has been added to the <code>java.beans</code> package. 107 * Please see {@link java.beans.XMLEncoder}. 108 * 109 * @see JRootPane 110 * @see #setDefaultCloseOperation 111 * @see java.awt.event.WindowListener#windowClosing 112 * @see javax.swing.RootPaneContainer 113 * 114 * @beaninfo 115 * attribute: isContainer true 116 * attribute: containerDelegate getContentPane 117 * description: A toplevel window which can be minimized to an icon. 118 * 119 * @author Jeff Dinkins 120 * @author Georges Saab 121 * @author David Kloba 122 */ 123 public class JFrame extends Frame implements WindowConstants, 124 Accessible, 125 RootPaneContainer, 126 TransferHandler.HasGetTransferHandler 127 { 128 /** 129 * The exit application default window close operation. If a window 130 * has this set as the close operation and is closed in an applet, 131 * a <code>SecurityException</code> may be thrown. 132 * It is recommended you only use this in an application. 133 * <p> 134 * @since 1.3 135 */ 136 public static final int EXIT_ON_CLOSE = 3; 137 138 /** 139 * Key into the AppContext, used to check if should provide decorations 140 * by default. 141 */ 142 private static final Object defaultLookAndFeelDecoratedKey = 143 new StringBuffer("JFrame.defaultLookAndFeelDecorated"); 144 145 private int defaultCloseOperation = HIDE_ON_CLOSE; 146 147 /** 148 * The <code>TransferHandler</code> for this frame. 149 */ 150 private TransferHandler transferHandler; 151 152 /** 153 * The <code>JRootPane</code> instance that manages the 154 * <code>contentPane</code> 155 * and optional <code>menuBar</code> for this frame, as well as the 156 * <code>glassPane</code>. 157 * 158 * @see JRootPane 159 * @see RootPaneContainer 160 */ 161 protected JRootPane rootPane; 162 163 /** 164 * If true then calls to <code>add</code> and <code>setLayout</code> 165 * will be forwarded to the <code>contentPane</code>. This is initially 166 * false, but is set to true when the <code>JFrame</code> is constructed. 167 * 168 * @see #isRootPaneCheckingEnabled 169 * @see #setRootPaneCheckingEnabled 170 * @see javax.swing.RootPaneContainer 171 */ 172 protected boolean rootPaneCheckingEnabled = false; 173 174 175 /** 176 * Constructs a new frame that is initially invisible. 177 * <p> 178 * This constructor sets the component's locale property to the value 179 * returned by <code>JComponent.getDefaultLocale</code>. 180 * 181 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 182 * returns true. 183 * @see java.awt.GraphicsEnvironment#isHeadless 184 * @see Component#setSize 185 * @see Component#setVisible 186 * @see JComponent#getDefaultLocale 187 */ 188 public JFrame() throws HeadlessException { 189 super(); 190 frameInit(); 191 } 192 193 /** 194 * Creates a <code>Frame</code> in the specified 195 * <code>GraphicsConfiguration</code> of 196 * a screen device and a blank title. 197 * <p> 198 * This constructor sets the component's locale property to the value 199 * returned by <code>JComponent.getDefaultLocale</code>. 200 * 201 * @param gc the <code>GraphicsConfiguration</code> that is used 202 * to construct the new <code>Frame</code>; 203 * if <code>gc</code> is <code>null</code>, the system 204 * default <code>GraphicsConfiguration</code> is assumed 205 * @exception IllegalArgumentException if <code>gc</code> is not from 206 * a screen device. This exception is always thrown when 207 * GraphicsEnvironment.isHeadless() returns true. 208 * @see java.awt.GraphicsEnvironment#isHeadless 209 * @see JComponent#getDefaultLocale 210 * @since 1.3 211 */ 212 public JFrame(GraphicsConfiguration gc) { 213 super(gc); 214 frameInit(); 215 } 216 217 /** 218 * Creates a new, initially invisible <code>Frame</code> with the 219 * specified title. 220 * <p> 221 * This constructor sets the component's locale property to the value 222 * returned by <code>JComponent.getDefaultLocale</code>. 223 * 224 * @param title the title for the frame 225 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 226 * returns true. 227 * @see java.awt.GraphicsEnvironment#isHeadless 228 * @see Component#setSize 229 * @see Component#setVisible 230 * @see JComponent#getDefaultLocale 231 */ 232 public JFrame(String title) throws HeadlessException { 233 super(title); 234 frameInit(); 235 } 236 237 /** 238 * Creates a <code>JFrame</code> with the specified title and the 239 * specified <code>GraphicsConfiguration</code> of a screen device. 240 * <p> 241 * This constructor sets the component's locale property to the value 242 * returned by <code>JComponent.getDefaultLocale</code>. 243 * 244 * @param title the title to be displayed in the 245 * frame's border. A <code>null</code> value is treated as 246 * an empty string, "". 247 * @param gc the <code>GraphicsConfiguration</code> that is used 248 * to construct the new <code>JFrame</code> with; 249 * if <code>gc</code> is <code>null</code>, the system 250 * default <code>GraphicsConfiguration</code> is assumed 251 * @exception IllegalArgumentException if <code>gc</code> is not from 252 * a screen device. This exception is always thrown when 253 * GraphicsEnvironment.isHeadless() returns true. 254 * @see java.awt.GraphicsEnvironment#isHeadless 255 * @see JComponent#getDefaultLocale 256 * @since 1.3 257 */ 258 public JFrame(String title, GraphicsConfiguration gc) { 259 super(title, gc); 260 frameInit(); 261 } 262 263 /** Called by the constructors to init the <code>JFrame</code> properly. */ 264 protected void frameInit() { 265 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK); 266 setLocale( JComponent.getDefaultLocale() ); 267 setRootPane(createRootPane()); 268 setBackground(UIManager.getColor("control")); 269 setRootPaneCheckingEnabled(true); 270 if (JFrame.isDefaultLookAndFeelDecorated()) { 271 boolean supportsWindowDecorations = 272 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 273 if (supportsWindowDecorations) { 274 setUndecorated(true); 275 getRootPane().setWindowDecorationStyle(JRootPane.FRAME); 276 } 277 } 278 sun.awt.SunToolkit.checkAndSetPolicy(this); 279 } 280 281 /** 282 * Called by the constructor methods to create the default 283 * <code>rootPane</code>. 284 */ 285 protected JRootPane createRootPane() { 286 JRootPane rp = new JRootPane(); 287 // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there 288 // is NO reason for the RootPane not to be opaque. For painting to 289 // work the contentPane must be opaque, therefor the RootPane can 290 // also be opaque. 291 rp.setOpaque(true); 292 return rp; 293 } 294 295 /** 296 * Processes window events occurring on this component. 297 * Hides the window or disposes of it, as specified by the setting 298 * of the <code>defaultCloseOperation</code> property. 299 * 300 * @param e the window event 301 * @see #setDefaultCloseOperation 302 * @see java.awt.Window#processWindowEvent 303 */ 304 protected void processWindowEvent(final WindowEvent e) { 305 super.processWindowEvent(e); 306 307 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 308 switch (defaultCloseOperation) { 309 case HIDE_ON_CLOSE: 310 setVisible(false); 311 break; 312 case DISPOSE_ON_CLOSE: 313 dispose(); 314 break; 315 case EXIT_ON_CLOSE: 316 // This needs to match the checkExit call in 317 // setDefaultCloseOperation 318 System.exit(0); 319 break; 320 case DO_NOTHING_ON_CLOSE: 321 default: 322 } 323 } 324 } 325 326 /** 327 * Sets the operation that will happen by default when 328 * the user initiates a "close" on this frame. 329 * You must specify one of the following choices: 330 * <br><br> 331 * <ul> 332 * <li><code>DO_NOTHING_ON_CLOSE</code> 333 * (defined in <code>WindowConstants</code>): 334 * Don't do anything; require the 335 * program to handle the operation in the <code>windowClosing</code> 336 * method of a registered <code>WindowListener</code> object. 337 * 338 * <li><code>HIDE_ON_CLOSE</code> 339 * (defined in <code>WindowConstants</code>): 340 * Automatically hide the frame after 341 * invoking any registered <code>WindowListener</code> 342 * objects. 343 * 344 * <li><code>DISPOSE_ON_CLOSE</code> 345 * (defined in <code>WindowConstants</code>): 346 * Automatically hide and dispose the 347 * frame after invoking any registered <code>WindowListener</code> 348 * objects. 349 * 350 * <li><code>EXIT_ON_CLOSE</code> 351 * (defined in <code>JFrame</code>): 352 * Exit the application using the <code>System</code> 353 * <code>exit</code> method. Use this only in applications. 354 * </ul> 355 * <p> 356 * The value is set to <code>HIDE_ON_CLOSE</code> by default. Changes 357 * to the value of this property cause the firing of a property 358 * change event, with property name "defaultCloseOperation". 359 * <p> 360 * <b>Note</b>: When the last displayable window within the 361 * Java virtual machine (VM) is disposed of, the VM may 362 * terminate. See <a href="../../java/awt/doc-files/AWTThreadIssues.html"> 363 * AWT Threading Issues</a> for more information. 364 * 365 * @param operation the operation which should be performed when the 366 * user closes the frame 367 * @exception IllegalArgumentException if defaultCloseOperation value 368 * isn't one of the above valid values 369 * @see #addWindowListener 370 * @see #getDefaultCloseOperation 371 * @see WindowConstants 372 * @throws SecurityException 373 * if <code>EXIT_ON_CLOSE</code> has been specified and the 374 * <code>SecurityManager</code> will 375 * not allow the caller to invoke <code>System.exit</code> 376 * @see java.lang.Runtime#exit(int) 377 * 378 * @beaninfo 379 * preferred: true 380 * bound: true 381 * enum: DO_NOTHING_ON_CLOSE WindowConstants.DO_NOTHING_ON_CLOSE 382 * HIDE_ON_CLOSE WindowConstants.HIDE_ON_CLOSE 383 * DISPOSE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE 384 * EXIT_ON_CLOSE WindowConstants.EXIT_ON_CLOSE 385 * description: The frame's default close operation. 386 */ 387 public void setDefaultCloseOperation(int operation) { 388 if (operation != DO_NOTHING_ON_CLOSE && 389 operation != HIDE_ON_CLOSE && 390 operation != DISPOSE_ON_CLOSE && 391 operation != EXIT_ON_CLOSE) { 392 throw new IllegalArgumentException("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE"); 393 } 394 395 if (operation == EXIT_ON_CLOSE) { 396 SecurityManager security = System.getSecurityManager(); 397 if (security != null) { 398 security.checkExit(0); 399 } 400 } 401 if (this.defaultCloseOperation != operation) { 402 int oldValue = this.defaultCloseOperation; 403 this.defaultCloseOperation = operation; 404 firePropertyChange("defaultCloseOperation", oldValue, operation); 405 } 406 } 407 408 409 /** 410 * Returns the operation that occurs when the user 411 * initiates a "close" on this frame. 412 * 413 * @return an integer indicating the window-close operation 414 * @see #setDefaultCloseOperation 415 */ 416 public int getDefaultCloseOperation() { 417 return defaultCloseOperation; 418 } 419 420 /** 421 * Sets the {@code transferHandler} property, which is a mechanism to 422 * support transfer of data into this component. Use {@code null} 423 * if the component does not support data transfer operations. 424 * <p> 425 * If the system property {@code suppressSwingDropSupport} is {@code false} 426 * (the default) and the current drop target on this component is either 427 * {@code null} or not a user-set drop target, this method will change the 428 * drop target as follows: If {@code newHandler} is {@code null} it will 429 * clear the drop target. If not {@code null} it will install a new 430 * {@code DropTarget}. 431 * <p> 432 * Note: When used with {@code JFrame}, {@code TransferHandler} only 433 * provides data import capability, as the data export related methods 434 * are currently typed to {@code JComponent}. 435 * <p> 436 * Please see 437 * <a href="https://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html"> 438 * How to Use Drag and Drop and Data Transfer</a>, a section in 439 * <em>The Java Tutorial</em>, for more information. 440 * 441 * @param newHandler the new {@code TransferHandler} 442 * 443 * @see TransferHandler 444 * @see #getTransferHandler 445 * @see java.awt.Component#setDropTarget 446 * @since 1.6 447 * 448 * @beaninfo 449 * bound: true 450 * hidden: true 451 * description: Mechanism for transfer of data into the component 452 */ 453 public void setTransferHandler(TransferHandler newHandler) { 454 TransferHandler oldHandler = transferHandler; 455 transferHandler = newHandler; 456 SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler); 457 firePropertyChange("transferHandler", oldHandler, newHandler); 458 } 459 460 /** 461 * Gets the <code>transferHandler</code> property. 462 * 463 * @return the value of the <code>transferHandler</code> property 464 * 465 * @see TransferHandler 466 * @see #setTransferHandler 467 * @since 1.6 468 */ 469 public TransferHandler getTransferHandler() { 470 return transferHandler; 471 } 472 473 /** 474 * Just calls <code>paint(g)</code>. This method was overridden to 475 * prevent an unnecessary call to clear the background. 476 * 477 * @param g the Graphics context in which to paint 478 */ 479 public void update(Graphics g) { 480 paint(g); 481 } 482 483 /** 484 * Sets the menubar for this frame. 485 * @param menubar the menubar being placed in the frame 486 * 487 * @see #getJMenuBar 488 * 489 * @beaninfo 490 * hidden: true 491 * description: The menubar for accessing pulldown menus from this frame. 492 */ 493 public void setJMenuBar(JMenuBar menubar) { 494 getRootPane().setMenuBar(menubar); 495 } 496 497 /** 498 * Returns the menubar set on this frame. 499 * @return the menubar for this frame 500 * 501 * @see #setJMenuBar 502 */ 503 public JMenuBar getJMenuBar() { 504 return getRootPane().getMenuBar(); 505 } 506 507 /** 508 * Returns whether calls to <code>add</code> and 509 * <code>setLayout</code> are forwarded to the <code>contentPane</code>. 510 * 511 * @return true if <code>add</code> and <code>setLayout</code> 512 * are forwarded; false otherwise 513 * 514 * @see #addImpl 515 * @see #setLayout 516 * @see #setRootPaneCheckingEnabled 517 * @see javax.swing.RootPaneContainer 518 */ 519 protected boolean isRootPaneCheckingEnabled() { 520 return rootPaneCheckingEnabled; 521 } 522 523 524 /** 525 * Sets whether calls to <code>add</code> and 526 * <code>setLayout</code> are forwarded to the <code>contentPane</code>. 527 * 528 * @param enabled true if <code>add</code> and <code>setLayout</code> 529 * are forwarded, false if they should operate directly on the 530 * <code>JFrame</code>. 531 * 532 * @see #addImpl 533 * @see #setLayout 534 * @see #isRootPaneCheckingEnabled 535 * @see javax.swing.RootPaneContainer 536 * @beaninfo 537 * hidden: true 538 * description: Whether the add and setLayout methods are forwarded 539 */ 540 protected void setRootPaneCheckingEnabled(boolean enabled) { 541 rootPaneCheckingEnabled = enabled; 542 } 543 544 545 /** 546 * Adds the specified child <code>Component</code>. 547 * This method is overridden to conditionally forward calls to the 548 * <code>contentPane</code>. 549 * By default, children are added to the <code>contentPane</code> instead 550 * of the frame, refer to {@link javax.swing.RootPaneContainer} for 551 * details. 552 * 553 * @param comp the component to be enhanced 554 * @param constraints the constraints to be respected 555 * @param index the index 556 * @exception IllegalArgumentException if <code>index</code> is invalid 557 * @exception IllegalArgumentException if adding the container's parent 558 * to itself 559 * @exception IllegalArgumentException if adding a window to a container 560 * 561 * @see #setRootPaneCheckingEnabled 562 * @see javax.swing.RootPaneContainer 563 */ 564 protected void addImpl(Component comp, Object constraints, int index) 565 { 566 if(isRootPaneCheckingEnabled()) { 567 getContentPane().add(comp, constraints, index); 568 } 569 else { 570 super.addImpl(comp, constraints, index); 571 } 572 } 573 574 /** 575 * Removes the specified component from the container. If 576 * <code>comp</code> is not the <code>rootPane</code>, this will forward 577 * the call to the <code>contentPane</code>. This will do nothing if 578 * <code>comp</code> is not a child of the <code>JFrame</code> or 579 * <code>contentPane</code>. 580 * 581 * @param comp the component to be removed 582 * @throws NullPointerException if <code>comp</code> is null 583 * @see #add 584 * @see javax.swing.RootPaneContainer 585 */ 586 public void remove(Component comp) { 587 if (comp == rootPane) { 588 super.remove(comp); 589 } else { 590 getContentPane().remove(comp); 591 } 592 } 593 594 595 /** 596 * Sets the <code>LayoutManager</code>. 597 * Overridden to conditionally forward the call to the 598 * <code>contentPane</code>. 599 * Refer to {@link javax.swing.RootPaneContainer} for 600 * more information. 601 * 602 * @param manager the <code>LayoutManager</code> 603 * @see #setRootPaneCheckingEnabled 604 * @see javax.swing.RootPaneContainer 605 */ 606 public void setLayout(LayoutManager manager) { 607 if(isRootPaneCheckingEnabled()) { 608 getContentPane().setLayout(manager); 609 } 610 else { 611 super.setLayout(manager); 612 } 613 } 614 615 616 /** 617 * Returns the <code>rootPane</code> object for this frame. 618 * @return the <code>rootPane</code> property 619 * 620 * @see #setRootPane 621 * @see RootPaneContainer#getRootPane 622 */ 623 public JRootPane getRootPane() { 624 return rootPane; 625 } 626 627 628 /** 629 * Sets the <code>rootPane</code> property. 630 * This method is called by the constructor. 631 * @param root the <code>rootPane</code> object for this frame 632 * 633 * @see #getRootPane 634 * 635 * @beaninfo 636 * hidden: true 637 * description: the RootPane object for this frame. 638 */ 639 protected void setRootPane(JRootPane root) 640 { 641 if(rootPane != null) { 642 remove(rootPane); 643 } 644 rootPane = root; 645 if(rootPane != null) { 646 boolean checkingEnabled = isRootPaneCheckingEnabled(); 647 try { 648 setRootPaneCheckingEnabled(false); 649 add(rootPane, BorderLayout.CENTER); 650 } 651 finally { 652 setRootPaneCheckingEnabled(checkingEnabled); 653 } 654 } 655 } 656 657 /** 658 * {@inheritDoc} 659 */ 660 public void setIconImage(Image image) { 661 super.setIconImage(image); 662 } 663 664 /** 665 * Returns the <code>contentPane</code> object for this frame. 666 * @return the <code>contentPane</code> property 667 * 668 * @see #setContentPane 669 * @see RootPaneContainer#getContentPane 670 */ 671 public Container getContentPane() { 672 return getRootPane().getContentPane(); 673 } 674 675 /** 676 * Sets the <code>contentPane</code> property. 677 * This method is called by the constructor. 678 * <p> 679 * Swing's painting architecture requires an opaque <code>JComponent</code> 680 * in the containment hierarchy. This is typically provided by the 681 * content pane. If you replace the content pane it is recommended you 682 * replace it with an opaque <code>JComponent</code>. 683 * 684 * @param contentPane the <code>contentPane</code> object for this frame 685 * 686 * @exception java.awt.IllegalComponentStateException (a runtime 687 * exception) if the content pane parameter is <code>null</code> 688 * @see #getContentPane 689 * @see RootPaneContainer#setContentPane 690 * @see JRootPane 691 * 692 * @beaninfo 693 * hidden: true 694 * description: The client area of the frame where child 695 * components are normally inserted. 696 */ 697 public void setContentPane(Container contentPane) { 698 getRootPane().setContentPane(contentPane); 699 } 700 701 /** 702 * Returns the <code>layeredPane</code> object for this frame. 703 * @return the <code>layeredPane</code> property 704 * 705 * @see #setLayeredPane 706 * @see RootPaneContainer#getLayeredPane 707 */ 708 public JLayeredPane getLayeredPane() { 709 return getRootPane().getLayeredPane(); 710 } 711 712 /** 713 * Sets the <code>layeredPane</code> property. 714 * This method is called by the constructor. 715 * @param layeredPane the <code>layeredPane</code> object for this frame 716 * 717 * @exception java.awt.IllegalComponentStateException (a runtime 718 * exception) if the layered pane parameter is <code>null</code> 719 * @see #getLayeredPane 720 * @see RootPaneContainer#setLayeredPane 721 * 722 * @beaninfo 723 * hidden: true 724 * description: The pane that holds the various frame layers. 725 */ 726 public void setLayeredPane(JLayeredPane layeredPane) { 727 getRootPane().setLayeredPane(layeredPane); 728 } 729 730 /** 731 * Returns the <code>glassPane</code> object for this frame. 732 * @return the <code>glassPane</code> property 733 * 734 * @see #setGlassPane 735 * @see RootPaneContainer#getGlassPane 736 */ 737 public Component getGlassPane() { 738 return getRootPane().getGlassPane(); 739 } 740 741 /** 742 * Sets the <code>glassPane</code> property. 743 * This method is called by the constructor. 744 * @param glassPane the <code>glassPane</code> object for this frame 745 * 746 * @see #getGlassPane 747 * @see RootPaneContainer#setGlassPane 748 * 749 * @beaninfo 750 * hidden: true 751 * description: A transparent pane used for menu rendering. 752 */ 753 public void setGlassPane(Component glassPane) { 754 getRootPane().setGlassPane(glassPane); 755 } 756 757 /** 758 * {@inheritDoc} 759 * 760 * @since 1.6 761 */ 762 public Graphics getGraphics() { 763 JComponent.getGraphicsInvoked(this); 764 return super.getGraphics(); 765 } 766 767 /** 768 * Repaints the specified rectangle of this component within 769 * <code>time</code> milliseconds. Refer to <code>RepaintManager</code> 770 * for details on how the repaint is handled. 771 * 772 * @param time maximum time in milliseconds before update 773 * @param x the <i>x</i> coordinate 774 * @param y the <i>y</i> coordinate 775 * @param width the width 776 * @param height the height 777 * @see RepaintManager 778 * @since 1.6 779 */ 780 public void repaint(long time, int x, int y, int width, int height) { 781 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) { 782 RepaintManager.currentManager(this).addDirtyRegion( 783 this, x, y, width, height); 784 } 785 else { 786 super.repaint(time, x, y, width, height); 787 } 788 } 789 790 /** 791 * Provides a hint as to whether or not newly created <code>JFrame</code>s 792 * should have their Window decorations (such as borders, widgets to 793 * close the window, title...) provided by the current look 794 * and feel. If <code>defaultLookAndFeelDecorated</code> is true, 795 * the current <code>LookAndFeel</code> supports providing window 796 * decorations, and the current window manager supports undecorated 797 * windows, then newly created <code>JFrame</code>s will have their 798 * Window decorations provided by the current <code>LookAndFeel</code>. 799 * Otherwise, newly created <code>JFrame</code>s will have their 800 * Window decorations provided by the current window manager. 801 * <p> 802 * You can get the same effect on a single JFrame by doing the following: 803 * <pre> 804 * JFrame frame = new JFrame(); 805 * frame.setUndecorated(true); 806 * frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); 807 * </pre> 808 * 809 * @param defaultLookAndFeelDecorated A hint as to whether or not current 810 * look and feel should provide window decorations 811 * @see javax.swing.LookAndFeel#getSupportsWindowDecorations 812 * @since 1.4 813 */ 814 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) { 815 if (defaultLookAndFeelDecorated) { 816 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE); 817 } else { 818 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE); 819 } 820 } 821 822 823 /** 824 * Returns true if newly created <code>JFrame</code>s should have their 825 * Window decorations provided by the current look and feel. This is only 826 * a hint, as certain look and feels may not support this feature. 827 * 828 * @return true if look and feel should provide Window decorations. 829 * @since 1.4 830 */ 831 public static boolean isDefaultLookAndFeelDecorated() { 832 Boolean defaultLookAndFeelDecorated = 833 (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey); 834 if (defaultLookAndFeelDecorated == null) { 835 defaultLookAndFeelDecorated = Boolean.FALSE; 836 } 837 return defaultLookAndFeelDecorated.booleanValue(); 838 } 839 840 /** 841 * Returns a string representation of this <code>JFrame</code>. 842 * This method 843 * is intended to be used only for debugging purposes, and the 844 * content and format of the returned string may vary between 845 * implementations. The returned string may be empty but may not 846 * be <code>null</code>. 847 * 848 * @return a string representation of this <code>JFrame</code> 849 */ 850 protected String paramString() { 851 String defaultCloseOperationString; 852 if (defaultCloseOperation == HIDE_ON_CLOSE) { 853 defaultCloseOperationString = "HIDE_ON_CLOSE"; 854 } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) { 855 defaultCloseOperationString = "DISPOSE_ON_CLOSE"; 856 } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) { 857 defaultCloseOperationString = "DO_NOTHING_ON_CLOSE"; 858 } else if (defaultCloseOperation == 3) { 859 defaultCloseOperationString = "EXIT_ON_CLOSE"; 860 } else defaultCloseOperationString = ""; 861 String rootPaneString = (rootPane != null ? 862 rootPane.toString() : ""); 863 String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ? 864 "true" : "false"); 865 866 return super.paramString() + 867 ",defaultCloseOperation=" + defaultCloseOperationString + 868 ",rootPane=" + rootPaneString + 869 ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString; 870 } 871 872 873 874 ///////////////// 875 // Accessibility support 876 //////////////// 877 878 /** The accessible context property. */ 879 protected AccessibleContext accessibleContext = null; 880 881 /** 882 * Gets the AccessibleContext associated with this JFrame. 883 * For JFrames, the AccessibleContext takes the form of an 884 * AccessibleJFrame. 885 * A new AccessibleJFrame instance is created if necessary. 886 * 887 * @return an AccessibleJFrame that serves as the 888 * AccessibleContext of this JFrame 889 */ 890 public AccessibleContext getAccessibleContext() { 891 if (accessibleContext == null) { 892 accessibleContext = new AccessibleJFrame(); 893 } 894 return accessibleContext; 895 } 896 897 /** 898 * This class implements accessibility support for the 899 * <code>JFrame</code> class. It provides an implementation of the 900 * Java Accessibility API appropriate to frame user-interface 901 * elements. 902 */ 903 protected class AccessibleJFrame extends AccessibleAWTFrame { 904 905 // AccessibleContext methods 906 /** 907 * Get the accessible name of this object. 908 * 909 * @return the localized name of the object -- can be null if this 910 * object does not have a name 911 */ 912 public String getAccessibleName() { 913 if (accessibleName != null) { 914 return accessibleName; 915 } else { 916 if (getTitle() == null) { 917 return super.getAccessibleName(); 918 } else { 919 return getTitle(); 920 } 921 } 922 } 923 924 /** 925 * Get the state of this object. 926 * 927 * @return an instance of AccessibleStateSet containing the current 928 * state set of the object 929 * @see AccessibleState 930 */ 931 public AccessibleStateSet getAccessibleStateSet() { 932 AccessibleStateSet states = super.getAccessibleStateSet(); 933 934 if (isResizable()) { 935 states.add(AccessibleState.RESIZABLE); 936 } 937 if (getFocusOwner() != null) { 938 states.add(AccessibleState.ACTIVE); 939 } 940 // FIXME: [[[WDW - should also return ICONIFIED and ICONIFIABLE 941 // if we can ever figure these out]]] 942 return states; 943 } 944 } // inner class AccessibleJFrame 945 }