Class JRootPane

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JRootPane
All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,Accessible

public classJRootPaneextendsJComponentimplementsAccessible
A lightweight container used behind the scenes byJFrame,JDialog,JWindow,JApplet, andJInternalFrame. For task-oriented information on functionality provided by root panes seeHow to Use Root Panes, a section inThe Java Tutorial.

The following image shows the relationships between the classes that use root panes.

The following text describes this graphic.

The "heavyweight" components (those that delegate to a peer, or native component on the host system) are shown with a darker, heavier box. The four heavyweight JFC/Swing containers (JFrame,JDialog,JWindow, andJApplet) are shown in relation to the AWT classes they extend. These four components are the only heavyweight containers in the Swing library. The lightweight containerJInternalFrame is also shown. All five of these JFC/Swing containers implement theRootPaneContainer interface, and they all delegate their operations to aJRootPane (shown with a little "handle" on top).
Note: TheJComponent methodgetRootPane can be used to obtain theJRootPane that contains a given component.

Example:

the following text describes this graphic.

The diagram at right shows the structure of aJRootPane. AJRootpane is made up of aglassPane, an optionalmenuBar, and acontentPane. (TheJLayeredPane manages themenuBar and thecontentPane.) TheglassPane sits over the top of everything, where it is in a position to intercept mouse movements. Since theglassPane (like thecontentPane) can be an arbitrary component, it is also possible to set up theglassPane for drawing. Lines and images on theglassPane can then range over the frames underneath without being limited by their boundaries.

Although themenuBar component is optional, thelayeredPane,contentPane, andglassPane always exist. Attempting to set them tonull generates an exception.

To add components to theJRootPane (other than the optional menu bar), you add the object to thecontentPane of theJRootPane, like this:

       rootPane.getContentPane().add(child);
The same principle holds true for setting layout managers, removing components, listing children, etc. All these methods are invoked on thecontentPane instead of on theJRootPane.
Note: The default layout manager for thecontentPane is aBorderLayout manager. However, theJRootPane uses a customLayoutManager. So, when you want to change the layout manager for the components you added to aJRootPane, be sure to use code like this:
    rootPane.getContentPane().setLayout(new BoxLayout());
If aJMenuBar component is set on theJRootPane, it is positioned along the upper edge of the frame. ThecontentPane is adjusted in location and size to fill the remaining area. (TheJMenuBar and thecontentPane are added to thelayeredPane component at theJLayeredPane.FRAME_CONTENT_LAYER layer.)

ThelayeredPane is the parent of all children in theJRootPane -- both as the direct parent of the menu and the grandparent of all components added to thecontentPane. It is an instance ofJLayeredPane, which provides the ability to add components at several layers. This capability is very useful when working with menu popups, dialog boxes, and dragging -- situations in which you need to place a component on top of all other components in the pane.

TheglassPane sits on top of all other components in theJRootPane. That provides a convenient place to draw above all other components, and makes it possible to intercept mouse events, which is useful both for dragging and for drawing. Developers can usesetVisible on theglassPane to control when theglassPane displays over the other children. By default theglassPane is not visible.

The customLayoutManager used byJRootPane ensures that:

  1. TheglassPane fills the entire viewable area of theJRootPane (bounds - insets).
  2. ThelayeredPane fills the entire viewable area of theJRootPane. (bounds - insets)
  3. ThemenuBar is positioned at the upper edge of thelayeredPane.
  4. ThecontentPane fills the entire viewable area, minus themenuBar, if present.
Any other views in theJRootPane view hierarchy are ignored.

If you replace theLayoutManager of theJRootPane, you are responsible for managing all of these views. So ordinarily you will want to be sure that you change the layout manager for thecontentPane rather than for theJRootPane itself!

The painting architecture of Swing requires an opaqueJComponent to exist in the containment hierarchy above all other components. This is typically provided by way of the content pane. If you replace the content pane, it is recommended that you make the content pane opaque by way ofsetOpaque(true). Additionally, if the content pane overridespaintComponent, it will need to completely fill in the background in an opaque color inpaintComponent.

Warning: Swing is not thread safe. For more information seeSwing's Threading Policy.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to thejava.beans package. Please seeXMLEncoder.

Since:
1.2
See Also:
  • Field Details

    • NONE

      public static final int NONE
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should not provide any sort of Window decorations.
      Since:
      1.4
      See Also:
    • FRAME

      public static final int FRAME
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should provide decorations appropriate for a Frame.
      Since:
      1.4
      See Also:
    • PLAIN_DIALOG

      public static final int PLAIN_DIALOG
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should provide decorations appropriate for a Dialog.
      Since:
      1.4
      See Also:
    • INFORMATION_DIALOG

      public static final int INFORMATION_DIALOG
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should provide decorations appropriate for a Dialog used to display an informational message.
      Since:
      1.4
      See Also:
    • ERROR_DIALOG

      public static final int ERROR_DIALOG
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should provide decorations appropriate for a Dialog used to display an error message.
      Since:
      1.4
      See Also:
    • COLOR_CHOOSER_DIALOG

      public static final int COLOR_CHOOSER_DIALOG
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should provide decorations appropriate for a Dialog used to display aJColorChooser.
      Since:
      1.4
      See Also:
    • FILE_CHOOSER_DIALOG

      public static final int FILE_CHOOSER_DIALOG
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should provide decorations appropriate for a Dialog used to display aJFileChooser.
      Since:
      1.4
      See Also:
    • QUESTION_DIALOG

      public static final int QUESTION_DIALOG
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should provide decorations appropriate for a Dialog used to present a question to the user.
      Since:
      1.4
      See Also:
    • WARNING_DIALOG

      public static final int WARNING_DIALOG
      Constant used for the windowDecorationStyle property. Indicates that theJRootPane should provide decorations appropriate for a Dialog used to display a warning message.
      Since:
      1.4
      See Also:
    • menuBar

      protected JMenuBar menuBar
      The menu bar.
    • contentPane

      protected Container contentPane
      The content pane.
    • layeredPane

      protected JLayeredPane layeredPane
      The layered pane that manages the menu bar and content pane.
    • glassPane

      protected Component glassPane
      The glass pane that overlays the menu bar and content pane, so it can intercept mouse movements and such.
    • defaultButton

      protected JButton defaultButton
      The button that gets activated when the pane has the focus and a UI-specific action like pressing theEnter key occurs.
  • Constructor Details

    • JRootPane

      public JRootPane()
      Creates aJRootPane, setting up itsglassPane,layeredPane, andcontentPane.
  • Method Details

    • setDoubleBuffered

      public void setDoubleBuffered(boolean aFlag)
      Sets whether this component should use a buffer to paint. If set to true, all the drawing from this component will be done in an offscreen painting buffer. The offscreen painting buffer will the be copied onto the screen. If aComponent is buffered and one of its ancestor is also buffered, the ancestor buffer will be used.
      Overrides:
      setDoubleBuffered in class JComponent
      Parameters:
      aFlag - if true, set this component to be double buffered
      Since:
      1.6
    • getWindowDecorationStyle

      public int getWindowDecorationStyle()
      Returns a constant identifying the type of Window decorations theJRootPane is providing.
      Returns:
      One ofNONE,FRAME,PLAIN_DIALOG,INFORMATION_DIALOG,ERROR_DIALOG,COLOR_CHOOSER_DIALOG,FILE_CHOOSER_DIALOG,QUESTION_DIALOG orWARNING_DIALOG.
      Since:
      1.4
      See Also:
    • setWindowDecorationStyle

      @BeanProperty(expert=true,visualUpdate=true,enumerationValues={"JRootPane.NONE","JRootPane.FRAME","JRootPane.PLAIN_DIALOG","JRootPane.INFORMATION_DIALOG","JRootPane.ERROR_DIALOG","JRootPane.COLOR_CHOOSER_DIALOG","JRootPane.FILE_CHOOSER_DIALOG","JRootPane.QUESTION_DIALOG","JRootPane.WARNING_DIALOG"},description="Identifies the type of Window decorations to provide")public void setWindowDecorationStyle(int windowDecorationStyle)
      Sets the type of Window decorations (such as borders, widgets for closing a Window, title ...) theJRootPane should provide. The default is to provide no Window decorations (NONE).

      This is only a hint, and some look and feels may not support this. This is a bound property.

      Parameters:
      windowDecorationStyle - Constant identifying Window decorations to provide.
      Throws:
      IllegalArgumentException - ifstyle is not one of:NONE,FRAME,PLAIN_DIALOG,INFORMATION_DIALOG,ERROR_DIALOG,COLOR_CHOOSER_DIALOG,FILE_CHOOSER_DIALOG,QUESTION_DIALOG, orWARNING_DIALOG.
      Since:
      1.4
      See Also:
    • getUI

      public RootPaneUI getUI()
      Returns the L&F object that renders this component.
      Overrides:
      getUI in class JComponent
      Returns:
      LabelUI object
      Since:
      1.3
    • setUI

      @BeanProperty(expert=true,hidden=true,visualUpdate=true,description="The UI object that implements the Component's LookAndFeel.")public void setUI(RootPaneUI ui)
      Sets the L&F object that renders this component.
      Parameters:
      ui - theLabelUI L&F object
      Since:
      1.3
      See Also:
    • updateUI

      public void updateUI()
      Resets the UI property to a value from the current look and feel.
      Overrides:
      updateUI in class JComponent
      See Also:
    • getUIClassID

      public String getUIClassID()
      Returns a string that specifies the name of the L&F class that renders this component.
      Overrides:
      getUIClassID in class JComponent
      Returns:
      the string "RootPaneUI"
      See Also:
    • createLayeredPane

      protected JLayeredPane createLayeredPane()
      Called by the constructor methods to create the defaultlayeredPane. Bt default it creates a newJLayeredPane.
      Returns:
      the defaultlayeredPane
    • createContentPane

      protected Container createContentPane()
      Called by the constructor methods to create the defaultcontentPane. By default this method creates a newJComponent add sets aBorderLayout as itsLayoutManager.
      Returns:
      the defaultcontentPane
    • createGlassPane

      protected Component createGlassPane()
      Called by the constructor methods to create the defaultglassPane. By default this method creates a newJComponent with visibility set to false.
      Returns:
      the defaultglassPane
    • createRootLayout

      protected LayoutManager createRootLayout()
      Called by the constructor methods to create the defaultlayoutManager.
      Returns:
      the defaultlayoutManager.
    • setJMenuBar

      public void setJMenuBar(JMenuBar menu)
      Adds or changes the menu bar used in the layered pane.
      Parameters:
      menu - theJMenuBar to add
    • setMenuBar

      @Deprecatedpublic void setMenuBar(JMenuBar menu)
      Deprecated.
      As of Swing version 1.0.3 replaced bysetJMenuBar(JMenuBar menu).
      Specifies the menu bar value.
      Parameters:
      menu - theJMenuBar to add.
    • getJMenuBar

      public JMenuBar getJMenuBar()
      Returns the menu bar from the layered pane.
      Returns:
      theJMenuBar used in the pane
    • getMenuBar

      @Deprecatedpublic JMenuBar getMenuBar()
      Deprecated.
      As of Swing version 1.0.3 replaced bygetJMenuBar().
      Returns the menu bar value.
      Returns:
      theJMenuBar used in the pane
    • setContentPane

      public void setContentPane(Container content)
      Sets the content pane -- the container that holds the components parented by the root pane.

      Swing's painting architecture requires an opaqueJComponent in the containment hierarchy. This is typically provided by the content pane. If you replace the content pane it is recommended you replace it with an opaqueJComponent.

      Parameters:
      content - theContainer to use for component-contents
      Throws:
      IllegalComponentStateException - (a runtime exception) if the content pane parameter isnull
    • getContentPane

      public Container getContentPane()
      Returns the content pane -- the container that holds the components parented by the root pane.
      Returns:
      theContainer that holds the component-contents
    • setLayeredPane

      public void setLayeredPane(JLayeredPane layered)
      Sets the layered pane for the root pane. The layered pane typically holds a content pane and an optionalJMenuBar.
      Parameters:
      layered - theJLayeredPane to use
      Throws:
      IllegalComponentStateException - (a runtime exception) if the layered pane parameter isnull
    • getLayeredPane

      public JLayeredPane getLayeredPane()
      Gets the layered pane used by the root pane. The layered pane typically holds a content pane and an optionalJMenuBar.
      Returns:
      theJLayeredPane currently in use
    • setGlassPane

      public void setGlassPane(Component glass)
      Sets a specifiedComponent to be the glass pane for this root pane. The glass pane should normally be a lightweight, transparent component, because it will be made visible when ever the root pane needs to grab input events.

      The new glass pane's visibility is changed to match that of the current glass pane. An implication of this is that care must be taken when you want to replace the glass pane and make it visible. Either of the following will work:

         root.setGlassPane(newGlassPane);   newGlassPane.setVisible(true);
      or:
         root.getGlassPane().setVisible(true);   root.setGlassPane(newGlassPane);

      Parameters:
      glass - theComponent to use as the glass pane for thisJRootPane
      Throws:
      NullPointerException - if theglass parameter isnull
    • getGlassPane

      public Component getGlassPane()
      Returns the current glass pane for thisJRootPane.
      Returns:
      the current glass pane
      See Also:
    • isValidateRoot

      public boolean isValidateRoot()
      If a descendant of thisJRootPane callsrevalidate, validate from here on down.

      Deferred requests to layout a component and its descendents again. For example, calls torevalidate, are pushed upwards to either aJRootPane or aJScrollPane because both classes overrideisValidateRoot to return true.

      Overrides:
      isValidateRoot in class JComponent
      Returns:
      true
      See Also:
    • isOptimizedDrawingEnabled

      public boolean isOptimizedDrawingEnabled()
      TheglassPane andcontentPane have the same bounds, which meansJRootPane does not tiles its children and this should return false. On the other hand, theglassPane is normally not visible, and so this can return true if theglassPane isn't visible. Therefore, the return value here depends upon the visibility of theglassPane.
      Overrides:
      isOptimizedDrawingEnabled in class JComponent
      Returns:
      true if this component's children don't overlap
    • setDefaultButton

      @BeanProperty(description="The button activated by default in this root pane")public void setDefaultButton(JButton defaultButton)
      Sets thedefaultButton property, which determines the current default button for thisJRootPane. The default button is the button which will be activated when a UI-defined activation event (typically theEnter key) occurs in the root pane regardless of whether or not the button has keyboard focus (unless there is another component within the root pane which consumes the activation event, such as aJTextPane). For default activation to work, the button must be an enabled descendent of the root pane when activation occurs. To remove a default button from this root pane, set this property tonull.
      Parameters:
      defaultButton - theJButton which is to be the default button
      See Also:
    • getDefaultButton

      public JButton getDefaultButton()
      Returns the value of thedefaultButton property.
      Returns:
      theJButton which is currently the default button
      See Also:
    • addImpl

      protected void addImpl(Component comp,Object constraints, int index)
      Overridden to enforce the position of the glass component as the zero child.
      Overrides:
      addImpl in class Container
      Parameters:
      comp - the component to be enhanced
      constraints - the constraints to be respected
      index - the index
      See Also:
    • paramString

      protected String paramString()
      Returns a string representation of thisJRootPane. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not benull.
      Overrides:
      paramString in class JComponent
      Returns:
      a string representation of thisJRootPane.
    • getAccessibleContext

      public AccessibleContext getAccessibleContext()
      Gets theAccessibleContext associated with thisJRootPane. For root panes, theAccessibleContext takes the form of anAccessibleJRootPane. A newAccessibleJRootPane instance is created if necessary.
      Specified by:
      getAccessibleContext in interface Accessible
      Overrides:
      getAccessibleContext in class Component
      Returns:
      anAccessibleJRootPane that serves as theAccessibleContext of thisJRootPane