1 /* 2 * Copyright (c) 1997, 2019, 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.applet.Applet; 28 import java.awt.*; 29 import java.awt.event.*; 30 import java.beans.*; 31 import java.security.AccessController; 32 import javax.accessibility.*; 33 import javax.swing.plaf.RootPaneUI; 34 import java.util.Vector; 35 import java.io.Serializable; 36 import javax.swing.border.*; 37 38 import sun.awt.AWTAccessor; 39 import sun.security.action.GetBooleanAction; 40 41 42 /** 43 * A lightweight container used behind the scenes by 44 * <code>JFrame</code>, <code>JDialog</code>, <code>JWindow</code>, 45 * <code>JApplet</code>, and <code>JInternalFrame</code>. 46 * For task-oriented information on functionality provided by root panes 47 * see <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html">How to Use Root Panes</a>, 48 * a section in <em>The Java Tutorial</em>. 49 * 50 * <p> 51 * The following image shows the relationships between 52 * the classes that use root panes. 53 * <p style="text-align:center"><img src="doc-files/JRootPane-1.gif" 54 * alt="The following text describes this graphic." 55 * HEIGHT=484 WIDTH=629></p> 56 * The "heavyweight" components (those that delegate to a peer, or native 57 * component on the host system) are shown with a darker, heavier box. The four 58 * heavyweight JFC/Swing containers (<code>JFrame</code>, <code>JDialog</code>, 59 * <code>JWindow</code>, and <code>JApplet</code>) are 60 * shown in relation to the AWT classes they extend. 61 * These four components are the 62 * only heavyweight containers in the Swing library. The lightweight container 63 * <code>JInternalFrame</code> is also shown. 64 * All five of these JFC/Swing containers implement the 65 * <code>RootPaneContainer</code> interface, 66 * and they all delegate their operations to a 67 * <code>JRootPane</code> (shown with a little "handle" on top). 68 * <blockquote> 69 * <b>Note:</b> The <code>JComponent</code> method <code>getRootPane</code> 70 * can be used to obtain the <code>JRootPane</code> that contains 71 * a given component. 72 * </blockquote> 73 * 74 * <div style="float:right;text-align:center;font-weight:bold"> 75 * <p>Example: 76 * <p><img src="doc-files/JRootPane-2.gif" 77 * alt="the following text describes this graphic." height=386 width=349> 78 * </div> 79 * The diagram at right shows the structure of a <code>JRootPane</code>. 80 * A <code>JRootpane</code> is made up of a <code>glassPane</code>, 81 * an optional <code>menuBar</code>, and a <code>contentPane</code>. 82 * (The <code>JLayeredPane</code> manages the <code>menuBar</code> 83 * and the <code>contentPane</code>.) 84 * The <code>glassPane</code> sits over the top of everything, 85 * where it is in a position to intercept mouse movements. 86 * Since the <code>glassPane</code> (like the <code>contentPane</code>) 87 * can be an arbitrary component, it is also possible to set up the 88 * <code>glassPane</code> for drawing. Lines and images on the 89 * <code>glassPane</code> can then range 90 * over the frames underneath without being limited by their boundaries. 91 * <p> 92 * Although the <code>menuBar</code> component is optional, 93 * the <code>layeredPane</code>, <code>contentPane</code>, 94 * and <code>glassPane</code> always exist. 95 * Attempting to set them to <code>null</code> generates an exception. 96 * <p> 97 * To add components to the <code>JRootPane</code> (other than the 98 * optional menu bar), you add the object to the <code>contentPane</code> 99 * of the <code>JRootPane</code>, like this: 100 * <pre> 101 * rootPane.getContentPane().add(child); 102 * </pre> 103 * The same principle holds true for setting layout managers, removing 104 * components, listing children, etc. All these methods are invoked on 105 * the <code>contentPane</code> instead of on the <code>JRootPane</code>. 106 * <blockquote> 107 * <b>Note:</b> The default layout manager for the <code>contentPane</code> is 108 * a <code>BorderLayout</code> manager. However, the <code>JRootPane</code> 109 * uses a custom <code>LayoutManager</code>. 110 * So, when you want to change the layout manager for the components you added 111 * to a <code>JRootPane</code>, be sure to use code like this: 112 * <pre> 113 * rootPane.getContentPane().setLayout(new BoxLayout()); 114 * </pre></blockquote> 115 * If a <code>JMenuBar</code> component is set on the <code>JRootPane</code>, 116 * it is positioned along the upper edge of the frame. 117 * The <code>contentPane</code> is adjusted in location and size to 118 * fill the remaining area. 119 * (The <code>JMenuBar</code> and the <code>contentPane</code> are added to the 120 * <code>layeredPane</code> component at the 121 * <code>JLayeredPane.FRAME_CONTENT_LAYER</code> layer.) 122 * <p> 123 * The <code>layeredPane</code> is the parent of all children in the 124 * <code>JRootPane</code> -- both as the direct parent of the menu and 125 * the grandparent of all components added to the <code>contentPane</code>. 126 * It is an instance of <code>JLayeredPane</code>, 127 * which provides the ability to add components at several layers. 128 * This capability is very useful when working with menu popups, 129 * dialog boxes, and dragging -- situations in which you need to place 130 * a component on top of all other components in the pane. 131 * <p> 132 * The <code>glassPane</code> sits on top of all other components in the 133 * <code>JRootPane</code>. 134 * That provides a convenient place to draw above all other components, 135 * and makes it possible to intercept mouse events, 136 * which is useful both for dragging and for drawing. 137 * Developers can use <code>setVisible</code> on the <code>glassPane</code> 138 * to control when the <code>glassPane</code> displays over the other children. 139 * By default the <code>glassPane</code> is not visible. 140 * <p> 141 * The custom <code>LayoutManager</code> used by <code>JRootPane</code> 142 * ensures that: 143 * <OL> 144 * <LI>The <code>glassPane</code> fills the entire viewable 145 * area of the <code>JRootPane</code> (bounds - insets). 146 * <LI>The <code>layeredPane</code> fills the entire viewable area of the 147 * <code>JRootPane</code>. (bounds - insets) 148 * <LI>The <code>menuBar</code> is positioned at the upper edge of the 149 * <code>layeredPane</code>. 150 * <LI>The <code>contentPane</code> fills the entire viewable area, 151 * minus the <code>menuBar</code>, if present. 152 * </OL> 153 * Any other views in the <code>JRootPane</code> view hierarchy are ignored. 154 * <p> 155 * If you replace the <code>LayoutManager</code> of the <code>JRootPane</code>, 156 * you are responsible for managing all of these views. 157 * So ordinarily you will want to be sure that you 158 * change the layout manager for the <code>contentPane</code> rather than 159 * for the <code>JRootPane</code> itself! 160 * <p> 161 * The painting architecture of Swing requires an opaque 162 * <code>JComponent</code> 163 * to exist in the containment hierarchy above all other components. This is 164 * typically provided by way of the content pane. If you replace the content 165 * pane, it is recommended that you make the content pane opaque 166 * by way of <code>setOpaque(true)</code>. Additionally, if the content pane 167 * overrides <code>paintComponent</code>, it 168 * will need to completely fill in the background in an opaque color in 169 * <code>paintComponent</code>. 170 * <p> 171 * <strong>Warning:</strong> Swing is not thread safe. For more 172 * information see <a 173 * href="package-summary.html#threading">Swing's Threading 174 * Policy</a>. 175 * <p> 176 * <strong>Warning:</strong> 177 * Serialized objects of this class will not be compatible with 178 * future Swing releases. The current serialization support is 179 * appropriate for short term storage or RMI between applications running 180 * the same version of Swing. As of 1.4, support for long term storage 181 * of all JavaBeans™ 182 * has been added to the <code>java.beans</code> package. 183 * Please see {@link java.beans.XMLEncoder}. 184 * 185 * @see JLayeredPane 186 * @see JMenuBar 187 * @see JWindow 188 * @see JFrame 189 * @see JDialog 190 * @see JApplet 191 * @see JInternalFrame 192 * @see JComponent 193 * @see BoxLayout 194 * 195 * @see <a href="http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html"> 196 * Mixing Heavy and Light Components</a> 197 * 198 * @author David Kloba 199 * @since 1.2 200 */ 201 /// PENDING(klobad) Who should be opaque in this component? 202 @SuppressWarnings("serial") 203 public class JRootPane extends JComponent implements Accessible { 204 205 private static final String uiClassID = "RootPaneUI"; 206 207 /** 208 * Whether or not we should dump the stack when true double buffering 209 * is disabled. Default is false. 210 */ 211 private static final boolean LOG_DISABLE_TRUE_DOUBLE_BUFFERING; 212 213 /** 214 * Whether or not we should ignore requests to disable true double 215 * buffering. Default is false. 216 */ 217 private static final boolean IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING; 218 219 /** 220 * Constant used for the windowDecorationStyle property. Indicates that 221 * the <code>JRootPane</code> should not provide any sort of 222 * Window decorations. 223 * 224 * @since 1.4 225 */ 226 public static final int NONE = 0; 227 228 /** 229 * Constant used for the windowDecorationStyle property. Indicates that 230 * the <code>JRootPane</code> should provide decorations appropriate for 231 * a Frame. 232 * 233 * @since 1.4 234 */ 235 public static final int FRAME = 1; 236 237 /** 238 * Constant used for the windowDecorationStyle property. Indicates that 239 * the <code>JRootPane</code> should provide decorations appropriate for 240 * a Dialog. 241 * 242 * @since 1.4 243 */ 244 public static final int PLAIN_DIALOG = 2; 245 246 /** 247 * Constant used for the windowDecorationStyle property. Indicates that 248 * the <code>JRootPane</code> should provide decorations appropriate for 249 * a Dialog used to display an informational message. 250 * 251 * @since 1.4 252 */ 253 public static final int INFORMATION_DIALOG = 3; 254 255 /** 256 * Constant used for the windowDecorationStyle property. Indicates that 257 * the <code>JRootPane</code> should provide decorations appropriate for 258 * a Dialog used to display an error message. 259 * 260 * @since 1.4 261 */ 262 public static final int ERROR_DIALOG = 4; 263 264 /** 265 * Constant used for the windowDecorationStyle property. Indicates that 266 * the <code>JRootPane</code> should provide decorations appropriate for 267 * a Dialog used to display a <code>JColorChooser</code>. 268 * 269 * @since 1.4 270 */ 271 public static final int COLOR_CHOOSER_DIALOG = 5; 272 273 /** 274 * Constant used for the windowDecorationStyle property. Indicates that 275 * the <code>JRootPane</code> should provide decorations appropriate for 276 * a Dialog used to display a <code>JFileChooser</code>. 277 * 278 * @since 1.4 279 */ 280 public static final int FILE_CHOOSER_DIALOG = 6; 281 282 /** 283 * Constant used for the windowDecorationStyle property. Indicates that 284 * the <code>JRootPane</code> should provide decorations appropriate for 285 * a Dialog used to present a question to the user. 286 * 287 * @since 1.4 288 */ 289 public static final int QUESTION_DIALOG = 7; 290 291 /** 292 * Constant used for the windowDecorationStyle property. Indicates that 293 * the <code>JRootPane</code> should provide decorations appropriate for 294 * a Dialog used to display a warning message. 295 * 296 * @since 1.4 297 */ 298 public static final int WARNING_DIALOG = 8; 299 300 private int windowDecorationStyle; 301 302 /** The menu bar. */ 303 protected JMenuBar menuBar; 304 305 /** The content pane. */ 306 protected Container contentPane; 307 308 /** The layered pane that manages the menu bar and content pane. */ 309 protected JLayeredPane layeredPane; 310 311 /** 312 * The glass pane that overlays the menu bar and content pane, 313 * so it can intercept mouse movements and such. 314 */ 315 protected Component glassPane; 316 /** 317 * The button that gets activated when the pane has the focus and 318 * a UI-specific action like pressing the <b>Enter</b> key occurs. 319 */ 320 protected JButton defaultButton; 321 322 /** 323 * Whether or not true double buffering should be used. This is typically 324 * true, but may be set to false in special situations. For example, 325 * heavy weight popups (backed by a window) set this to false. 326 */ 327 boolean useTrueDoubleBuffering = true; 328 329 static { 330 LOG_DISABLE_TRUE_DOUBLE_BUFFERING = 331 AccessController.doPrivileged(new GetBooleanAction( 332 "swing.logDoubleBufferingDisable")); 333 IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING = 334 AccessController.doPrivileged(new GetBooleanAction( 335 "swing.ignoreDoubleBufferingDisable")); 336 } 337 338 /** 339 * Creates a <code>JRootPane</code>, setting up its 340 * <code>glassPane</code>, <code>layeredPane</code>, 341 * and <code>contentPane</code>. 342 */ 343 public JRootPane() { 344 setGlassPane(createGlassPane()); 345 setLayeredPane(createLayeredPane()); 346 setContentPane(createContentPane()); 347 setLayout(createRootLayout()); 348 setDoubleBuffered(true); 349 updateUI(); 350 } 351 352 /** 353 * {@inheritDoc} 354 * @since 1.6 355 */ 356 public void setDoubleBuffered(boolean aFlag) { 357 if (isDoubleBuffered() != aFlag) { 358 super.setDoubleBuffered(aFlag); 359 RepaintManager.currentManager(this).doubleBufferingChanged(this); 360 } 361 } 362 363 /** 364 * Returns a constant identifying the type of Window decorations the 365 * <code>JRootPane</code> is providing. 366 * 367 * @return One of <code>NONE</code>, <code>FRAME</code>, 368 * <code>PLAIN_DIALOG</code>, <code>INFORMATION_DIALOG</code>, 369 * <code>ERROR_DIALOG</code>, <code>COLOR_CHOOSER_DIALOG</code>, 370 * <code>FILE_CHOOSER_DIALOG</code>, <code>QUESTION_DIALOG</code> or 371 * <code>WARNING_DIALOG</code>. 372 * @see #setWindowDecorationStyle 373 * @since 1.4 374 */ 375 public int getWindowDecorationStyle() { 376 return windowDecorationStyle; 377 } 378 379 /** 380 * Sets the type of Window decorations (such as borders, widgets for 381 * closing a Window, title ...) the <code>JRootPane</code> should 382 * provide. The default is to provide no Window decorations 383 * (<code>NONE</code>). 384 * <p> 385 * This is only a hint, and some look and feels may not support 386 * this. 387 * This is a bound property. 388 * 389 * @param windowDecorationStyle Constant identifying Window decorations 390 * to provide. 391 * @see JDialog#setDefaultLookAndFeelDecorated 392 * @see JFrame#setDefaultLookAndFeelDecorated 393 * @see LookAndFeel#getSupportsWindowDecorations 394 * @throws IllegalArgumentException if <code>style</code> is 395 * not one of: <code>NONE</code>, <code>FRAME</code>, 396 * <code>PLAIN_DIALOG</code>, <code>INFORMATION_DIALOG</code>, 397 * <code>ERROR_DIALOG</code>, <code>COLOR_CHOOSER_DIALOG</code>, 398 * <code>FILE_CHOOSER_DIALOG</code>, <code>QUESTION_DIALOG</code>, or 399 * <code>WARNING_DIALOG</code>. 400 * @since 1.4 401 */ 402 @BeanProperty(expert = true, visualUpdate = true, enumerationValues = { 403 "JRootPane.NONE", 404 "JRootPane.FRAME", 405 "JRootPane.PLAIN_DIALOG", 406 "JRootPane.INFORMATION_DIALOG", 407 "JRootPane.ERROR_DIALOG", 408 "JRootPane.COLOR_CHOOSER_DIALOG", 409 "JRootPane.FILE_CHOOSER_DIALOG", 410 "JRootPane.QUESTION_DIALOG", 411 "JRootPane.WARNING_DIALOG"}, description 412 = "Identifies the type of Window decorations to provide") 413 public void setWindowDecorationStyle(int windowDecorationStyle) { 414 if (windowDecorationStyle < 0 || 415 windowDecorationStyle > WARNING_DIALOG) { 416 throw new IllegalArgumentException("Invalid decoration style"); 417 } 418 int oldWindowDecorationStyle = getWindowDecorationStyle(); 419 this.windowDecorationStyle = windowDecorationStyle; 420 firePropertyChange("windowDecorationStyle", 421 oldWindowDecorationStyle, 422 windowDecorationStyle); 423 } 424 425 /** 426 * Returns the L&F object that renders this component. 427 * 428 * @return <code>LabelUI</code> object 429 * @since 1.3 430 */ 431 public RootPaneUI getUI() { 432 return (RootPaneUI)ui; 433 } 434 435 /** 436 * Sets the L&F object that renders this component. 437 * 438 * @param ui the <code>LabelUI</code> L&F object 439 * @see UIDefaults#getUI 440 * @since 1.3 441 */ 442 @BeanProperty(expert = true, hidden = true, visualUpdate = true, description 443 = "The UI object that implements the Component's LookAndFeel.") 444 public void setUI(RootPaneUI ui) { 445 super.setUI(ui); 446 } 447 448 449 /** 450 * Resets the UI property to a value from the current look and feel. 451 * 452 * @see JComponent#updateUI 453 */ 454 public void updateUI() { 455 setUI((RootPaneUI)UIManager.getUI(this)); 456 } 457 458 459 /** 460 * Returns a string that specifies the name of the L&F class 461 * that renders this component. 462 * 463 * @return the string "RootPaneUI" 464 * 465 * @see JComponent#getUIClassID 466 * @see UIDefaults#getUI 467 */ 468 public String getUIClassID() { 469 return uiClassID; 470 } 471 472 /** 473 * Called by the constructor methods to create the default 474 * <code>layeredPane</code>. 475 * Bt default it creates a new <code>JLayeredPane</code>. 476 * @return the default <code>layeredPane</code> 477 */ 478 protected JLayeredPane createLayeredPane() { 479 JLayeredPane p = new JLayeredPane(); 480 p.setName(this.getName()+".layeredPane"); 481 return p; 482 } 483 484 /** 485 * Called by the constructor methods to create the default 486 * <code>contentPane</code>. 487 * By default this method creates a new <code>JComponent</code> add sets a 488 * <code>BorderLayout</code> as its <code>LayoutManager</code>. 489 * @return the default <code>contentPane</code> 490 */ 491 protected Container createContentPane() { 492 JComponent c = new JPanel(); 493 c.setName(this.getName()+".contentPane"); 494 c.setLayout(new BorderLayout() { 495 /* This BorderLayout subclass maps a null constraint to CENTER. 496 * Although the reference BorderLayout also does this, some VMs 497 * throw an IllegalArgumentException. 498 */ 499 public void addLayoutComponent(Component comp, Object constraints) { 500 if (constraints == null) { 501 constraints = BorderLayout.CENTER; 502 } 503 super.addLayoutComponent(comp, constraints); 504 } 505 }); 506 return c; 507 } 508 509 /** 510 * Called by the constructor methods to create the default 511 * <code>glassPane</code>. 512 * By default this method creates a new <code>JComponent</code> 513 * with visibility set to false. 514 * @return the default <code>glassPane</code> 515 */ 516 protected Component createGlassPane() { 517 JComponent c = new JPanel(); 518 c.setName(this.getName()+".glassPane"); 519 c.setVisible(false); 520 ((JPanel)c).setOpaque(false); 521 return c; 522 } 523 524 /** 525 * Called by the constructor methods to create the default 526 * <code>layoutManager</code>. 527 * @return the default <code>layoutManager</code>. 528 */ 529 protected LayoutManager createRootLayout() { 530 return new RootLayout(); 531 } 532 533 /** 534 * Adds or changes the menu bar used in the layered pane. 535 * @param menu the <code>JMenuBar</code> to add 536 */ 537 public void setJMenuBar(JMenuBar menu) { 538 if(menuBar != null && menuBar.getParent() == layeredPane) 539 layeredPane.remove(menuBar); 540 menuBar = menu; 541 542 if(menuBar != null) { 543 menuBar.updateUI(); 544 layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER); 545 } 546 } 547 548 /** 549 * Specifies the menu bar value. 550 * @deprecated As of Swing version 1.0.3 551 * replaced by <code>setJMenuBar(JMenuBar menu)</code>. 552 * @param menu the <code>JMenuBar</code> to add. 553 */ 554 @Deprecated 555 public void setMenuBar(JMenuBar menu){ 556 if(menuBar != null && menuBar.getParent() == layeredPane) 557 layeredPane.remove(menuBar); 558 menuBar = menu; 559 560 if(menuBar != null) 561 layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER); 562 } 563 564 /** 565 * Returns the menu bar from the layered pane. 566 * @return the <code>JMenuBar</code> used in the pane 567 */ 568 public JMenuBar getJMenuBar() { return menuBar; } 569 570 /** 571 * Returns the menu bar value. 572 * @deprecated As of Swing version 1.0.3 573 * replaced by <code>getJMenuBar()</code>. 574 * @return the <code>JMenuBar</code> used in the pane 575 */ 576 @Deprecated 577 public JMenuBar getMenuBar() { return menuBar; } 578 579 /** 580 * Sets the content pane -- the container that holds the components 581 * parented by the root pane. 582 * <p> 583 * Swing's painting architecture requires an opaque <code>JComponent</code> 584 * in the containment hierarchy. This is typically provided by the 585 * content pane. If you replace the content pane it is recommended you 586 * replace it with an opaque <code>JComponent</code>. 587 * 588 * @param content the <code>Container</code> to use for component-contents 589 * @exception java.awt.IllegalComponentStateException (a runtime 590 * exception) if the content pane parameter is <code>null</code> 591 */ 592 public void setContentPane(Container content) { 593 if(content == null) 594 throw new IllegalComponentStateException("contentPane cannot be set to null."); 595 if(contentPane != null && contentPane.getParent() == layeredPane) 596 layeredPane.remove(contentPane); 597 contentPane = content; 598 599 layeredPane.add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER); 600 } 601 602 /** 603 * Returns the content pane -- the container that holds the components 604 * parented by the root pane. 605 * 606 * @return the <code>Container</code> that holds the component-contents 607 */ 608 public Container getContentPane() { return contentPane; } 609 610 // PENDING(klobad) Should this reparent the contentPane and MenuBar? 611 /** 612 * Sets the layered pane for the root pane. The layered pane 613 * typically holds a content pane and an optional <code>JMenuBar</code>. 614 * 615 * @param layered the <code>JLayeredPane</code> to use 616 * @exception java.awt.IllegalComponentStateException (a runtime 617 * exception) if the layered pane parameter is <code>null</code> 618 */ 619 public void setLayeredPane(JLayeredPane layered) { 620 if(layered == null) 621 throw new IllegalComponentStateException("layeredPane cannot be set to null."); 622 if(layeredPane != null && layeredPane.getParent() == this) 623 this.remove(layeredPane); 624 layeredPane = layered; 625 626 this.add(layeredPane, -1); 627 } 628 /** 629 * Gets the layered pane used by the root pane. The layered pane 630 * typically holds a content pane and an optional <code>JMenuBar</code>. 631 * 632 * @return the <code>JLayeredPane</code> currently in use 633 */ 634 public JLayeredPane getLayeredPane() { return layeredPane; } 635 636 /** 637 * Sets a specified <code>Component</code> to be the glass pane for this 638 * root pane. The glass pane should normally be a lightweight, 639 * transparent component, because it will be made visible when 640 * ever the root pane needs to grab input events. 641 * <p> 642 * The new glass pane's visibility is changed to match that of 643 * the current glass pane. An implication of this is that care 644 * must be taken when you want to replace the glass pane and 645 * make it visible. Either of the following will work: 646 * <pre> 647 * root.setGlassPane(newGlassPane); 648 * newGlassPane.setVisible(true); 649 * </pre> 650 * or: 651 * <pre> 652 * root.getGlassPane().setVisible(true); 653 * root.setGlassPane(newGlassPane); 654 * </pre> 655 * 656 * @param glass the <code>Component</code> to use as the glass pane 657 * for this <code>JRootPane</code> 658 * @exception NullPointerException if the <code>glass</code> parameter is 659 * <code>null</code> 660 */ 661 public void setGlassPane(Component glass) { 662 if (glass == null) { 663 throw new NullPointerException("glassPane cannot be set to null."); 664 } 665 666 glass.setMixingCutoutShape(new Rectangle()); 667 668 boolean visible = false; 669 if (glassPane != null && glassPane.getParent() == this) { 670 this.remove(glassPane); 671 visible = glassPane.isVisible(); 672 } 673 674 glass.setVisible(visible); 675 glassPane = glass; 676 this.add(glassPane, 0); 677 if (visible) { 678 repaint(); 679 } 680 } 681 682 /** 683 * Returns the current glass pane for this <code>JRootPane</code>. 684 * @return the current glass pane 685 * @see #setGlassPane 686 */ 687 public Component getGlassPane() { 688 return glassPane; 689 } 690 691 /** 692 * If a descendant of this <code>JRootPane</code> calls 693 * <code>revalidate</code>, validate from here on down. 694 *<p> 695 * Deferred requests to layout a component and its descendents again. 696 * For example, calls to <code>revalidate</code>, are pushed upwards to 697 * either a <code>JRootPane</code> or a <code>JScrollPane</code> 698 * because both classes override <code>isValidateRoot</code> to return true. 699 * 700 * @see JComponent#isValidateRoot 701 * @see java.awt.Container#isValidateRoot 702 * @return true 703 */ 704 @Override 705 public boolean isValidateRoot() { 706 return true; 707 } 708 709 /** 710 * The <code>glassPane</code> and <code>contentPane</code> 711 * have the same bounds, which means <code>JRootPane</code> 712 * does not tiles its children and this should return false. 713 * On the other hand, the <code>glassPane</code> 714 * is normally not visible, and so this can return true if the 715 * <code>glassPane</code> isn't visible. Therefore, the 716 * return value here depends upon the visibility of the 717 * <code>glassPane</code>. 718 * 719 * @return true if this component's children don't overlap 720 */ 721 public boolean isOptimizedDrawingEnabled() { 722 return !glassPane.isVisible(); 723 } 724 725 /** 726 * {@inheritDoc} 727 */ 728 public void addNotify() { 729 super.addNotify(); 730 enableEvents(AWTEvent.KEY_EVENT_MASK); 731 } 732 733 /** 734 * {@inheritDoc} 735 */ 736 public void removeNotify() { 737 super.removeNotify(); 738 } 739 740 741 /** 742 * Sets the <code>defaultButton</code> property, 743 * which determines the current default button for this <code>JRootPane</code>. 744 * The default button is the button which will be activated 745 * when a UI-defined activation event (typically the <b>Enter</b> key) 746 * occurs in the root pane regardless of whether or not the button 747 * has keyboard focus (unless there is another component within 748 * the root pane which consumes the activation event, 749 * such as a <code>JTextPane</code>). 750 * For default activation to work, the button must be an enabled 751 * descendent of the root pane when activation occurs. 752 * To remove a default button from this root pane, set this 753 * property to <code>null</code>. 754 * 755 * @see JButton#isDefaultButton 756 * @param defaultButton the <code>JButton</code> which is to be the default button 757 */ 758 @BeanProperty(description 759 = "The button activated by default in this root pane") 760 public void setDefaultButton(JButton defaultButton) { 761 JButton oldDefault = this.defaultButton; 762 763 if (oldDefault != defaultButton) { 764 this.defaultButton = defaultButton; 765 766 if (oldDefault != null) { 767 oldDefault.repaint(); 768 } 769 if (defaultButton != null) { 770 defaultButton.repaint(); 771 } 772 } 773 774 firePropertyChange("defaultButton", oldDefault, defaultButton); 775 } 776 777 /** 778 * Returns the value of the <code>defaultButton</code> property. 779 * @return the <code>JButton</code> which is currently the default button 780 * @see #setDefaultButton 781 */ 782 public JButton getDefaultButton() { 783 return defaultButton; 784 } 785 786 final void setUseTrueDoubleBuffering(boolean useTrueDoubleBuffering) { 787 this.useTrueDoubleBuffering = useTrueDoubleBuffering; 788 } 789 790 final boolean getUseTrueDoubleBuffering() { 791 return useTrueDoubleBuffering; 792 } 793 794 final void disableTrueDoubleBuffering() { 795 if (useTrueDoubleBuffering) { 796 if (!IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING) { 797 if (LOG_DISABLE_TRUE_DOUBLE_BUFFERING) { 798 System.out.println("Disabling true double buffering for " + 799 this); 800 Thread.dumpStack(); 801 } 802 useTrueDoubleBuffering = false; 803 RepaintManager.currentManager(this). 804 doubleBufferingChanged(this); 805 } 806 } 807 } 808 809 /** 810 * Overridden to enforce the position of the glass component as 811 * the zero child. 812 * 813 * @param comp the component to be enhanced 814 * @param constraints the constraints to be respected 815 * @param index the index 816 */ 817 protected void addImpl(Component comp, Object constraints, int index) { 818 super.addImpl(comp, constraints, index); 819 820 /// We are making sure the glassPane is on top. 821 if(glassPane != null 822 && glassPane.getParent() == this 823 && getComponent(0) != glassPane) { 824 add(glassPane, 0); 825 } 826 } 827 828 829 /////////////////////////////////////////////////////////////////////////////// 830 //// Begin Inner Classes 831 /////////////////////////////////////////////////////////////////////////////// 832 833 834 /** 835 * A custom layout manager that is responsible for the layout of 836 * layeredPane, glassPane, and menuBar. 837 * <p> 838 * <strong>Warning:</strong> 839 * Serialized objects of this class will not be compatible with 840 * future Swing releases. The current serialization support is 841 * appropriate for short term storage or RMI between applications running 842 * the same version of Swing. As of 1.4, support for long term storage 843 * of all JavaBeans™ 844 * has been added to the <code>java.beans</code> package. 845 * Please see {@link java.beans.XMLEncoder}. 846 */ 847 @SuppressWarnings("serial") 848 protected class RootLayout implements LayoutManager2, Serializable 849 { 850 /** 851 * Returns the amount of space the layout would like to have. 852 * 853 * @param parent the Container for which this layout manager 854 * is being used 855 * @return a Dimension object containing the layout's preferred size 856 */ 857 public Dimension preferredLayoutSize(Container parent) { 858 Dimension rd, mbd; 859 Insets i = getInsets(); 860 861 if(contentPane != null) { 862 rd = contentPane.getPreferredSize(); 863 } else { 864 rd = parent.getSize(); 865 } 866 if(menuBar != null && menuBar.isVisible()) { 867 mbd = menuBar.getPreferredSize(); 868 } else { 869 mbd = new Dimension(0, 0); 870 } 871 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 872 rd.height + mbd.height + i.top + i.bottom); 873 } 874 875 /** 876 * Returns the minimum amount of space the layout needs. 877 * 878 * @param parent the Container for which this layout manager 879 * is being used 880 * @return a Dimension object containing the layout's minimum size 881 */ 882 public Dimension minimumLayoutSize(Container parent) { 883 Dimension rd, mbd; 884 Insets i = getInsets(); 885 if(contentPane != null) { 886 rd = contentPane.getMinimumSize(); 887 } else { 888 rd = parent.getSize(); 889 } 890 if(menuBar != null && menuBar.isVisible()) { 891 mbd = menuBar.getMinimumSize(); 892 } else { 893 mbd = new Dimension(0, 0); 894 } 895 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 896 rd.height + mbd.height + i.top + i.bottom); 897 } 898 899 /** 900 * Returns the maximum amount of space the layout can use. 901 * 902 * @param target the Container for which this layout manager 903 * is being used 904 * @return a Dimension object containing the layout's maximum size 905 */ 906 public Dimension maximumLayoutSize(Container target) { 907 Dimension rd, mbd; 908 Insets i = getInsets(); 909 if(menuBar != null && menuBar.isVisible()) { 910 mbd = menuBar.getMaximumSize(); 911 } else { 912 mbd = new Dimension(0, 0); 913 } 914 if(contentPane != null) { 915 rd = contentPane.getMaximumSize(); 916 } else { 917 // This is silly, but should stop an overflow error 918 rd = new Dimension(Integer.MAX_VALUE, 919 Integer.MAX_VALUE - i.top - i.bottom - mbd.height - 1); 920 } 921 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 922 rd.height + mbd.height + i.top + i.bottom); 923 } 924 925 /** 926 * Instructs the layout manager to perform the layout for the specified 927 * container. 928 * 929 * @param parent the Container for which this layout manager 930 * is being used 931 */ 932 public void layoutContainer(Container parent) { 933 Rectangle b = parent.getBounds(); 934 Insets i = getInsets(); 935 int contentY = 0; 936 int w = b.width - i.right - i.left; 937 int h = b.height - i.top - i.bottom; 938 939 if(layeredPane != null) { 940 layeredPane.setBounds(i.left, i.top, w, h); 941 } 942 if(glassPane != null) { 943 glassPane.setBounds(i.left, i.top, w, h); 944 } 945 // Note: This is laying out the children in the layeredPane, 946 // technically, these are not our children. 947 if(menuBar != null && menuBar.isVisible()) { 948 Dimension mbd = menuBar.getPreferredSize(); 949 menuBar.setBounds(0, 0, w, mbd.height); 950 contentY += mbd.height; 951 } 952 if(contentPane != null) { 953 contentPane.setBounds(0, contentY, w, h - contentY); 954 } 955 } 956 957 public void addLayoutComponent(String name, Component comp) {} 958 public void removeLayoutComponent(Component comp) {} 959 public void addLayoutComponent(Component comp, Object constraints) {} 960 public float getLayoutAlignmentX(Container target) { return 0.0f; } 961 public float getLayoutAlignmentY(Container target) { return 0.0f; } 962 public void invalidateLayout(Container target) {} 963 } 964 965 /** 966 * Returns a string representation of this <code>JRootPane</code>. 967 * This method is intended to be used only for debugging purposes, 968 * and the content and format of the returned string may vary between 969 * implementations. The returned string may be empty but may not 970 * be <code>null</code>. 971 * 972 * @return a string representation of this <code>JRootPane</code>. 973 */ 974 protected String paramString() { 975 return super.paramString(); 976 } 977 978 ///////////////// 979 // Accessibility support 980 //////////////// 981 982 /** 983 * Gets the <code>AccessibleContext</code> associated with this 984 * <code>JRootPane</code>. For root panes, the 985 * <code>AccessibleContext</code> takes the form of an 986 * <code>AccessibleJRootPane</code>. 987 * A new <code>AccessibleJRootPane</code> instance is created if necessary. 988 * 989 * @return an <code>AccessibleJRootPane</code> that serves as the 990 * <code>AccessibleContext</code> of this <code>JRootPane</code> 991 */ 992 public AccessibleContext getAccessibleContext() { 993 if (accessibleContext == null) { 994 accessibleContext = new AccessibleJRootPane(); 995 } 996 return accessibleContext; 997 } 998 999 /** 1000 * This class implements accessibility support for the 1001 * <code>JRootPane</code> class. It provides an implementation of the 1002 * Java Accessibility API appropriate to root pane user-interface elements. 1003 * <p> 1004 * <strong>Warning:</strong> 1005 * Serialized objects of this class will not be compatible with 1006 * future Swing releases. The current serialization support is 1007 * appropriate for short term storage or RMI between applications running 1008 * the same version of Swing. As of 1.4, support for long term storage 1009 * of all JavaBeans™ 1010 * has been added to the <code>java.beans</code> package. 1011 * Please see {@link java.beans.XMLEncoder}. 1012 */ 1013 @SuppressWarnings("serial") 1014 protected class AccessibleJRootPane extends AccessibleJComponent { 1015 /** 1016 * Get the role of this object. 1017 * 1018 * @return an instance of AccessibleRole describing the role of 1019 * the object 1020 */ 1021 public AccessibleRole getAccessibleRole() { 1022 return AccessibleRole.ROOT_PANE; 1023 } 1024 1025 /** 1026 * Returns the number of accessible children of the object. 1027 * 1028 * @return the number of accessible children of the object. 1029 */ 1030 public int getAccessibleChildrenCount() { 1031 return super.getAccessibleChildrenCount(); 1032 } 1033 1034 /** 1035 * Returns the specified Accessible child of the object. The Accessible 1036 * children of an Accessible object are zero-based, so the first child 1037 * of an Accessible child is at index 0, the second child is at index 1, 1038 * and so on. 1039 * 1040 * @param i zero-based index of child 1041 * @return the Accessible child of the object 1042 * @see #getAccessibleChildrenCount 1043 */ 1044 public Accessible getAccessibleChild(int i) { 1045 return super.getAccessibleChild(i); 1046 } 1047 } // inner class AccessibleJRootPane 1048 }