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