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