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