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