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