JSR-209 (Final Release)

javax.swing
Class JRootPane

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JRootPane
All Implemented Interfaces:
java.awt.image.ImageObserver, java.io.Serializable

public class JRootPane
extends JComponent

A lightweight container used behind the scenes by JInternalFrame. For task-oriented information on functionality provided by root panes see How to Use Root Panes, a section in The Java Tutorial.

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

The following text describes this graphic.

The lightweight container JInternalPane is shown. JFC/Swing containers implement the RootPaneContainer interface, and they all delegate their operations to a JRootPane (shown with a little "handle" on top).
Note: The JComponent method getRootPane can be used to obtain the JRootPane that contains a given component.
The following text describes this graphic.
The diagram at right shows the structure of a JRootPane. A JRootpane is made up of a glassPane, an optional menuBar, and a contentPane. (The JLayeredPane manages the menuBar and the contentPane.) The glassPane sits over the top of everything, where it is in a position to intercept mouse movements. Since the glassPane (like the contentPane) can be an arbitrary component, it is also possible to set up the glassPane for drawing. Lines and images on the glassPane can then range over the frames underneath without being limited by their boundaries.

Although the menuBar component is optional, the layeredPane, contentPane, and glassPane always exist. Attempting to set them to null generates an exception.

To add components to the JRootPane (other than the optional menu bar), you add the object to the contentPane of the JRootPane, 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 the contentPane instead of on the JRootPane.
Note: The default layout manager for the contentPane is a BorderLayout manager. However, the JRootPane uses a custom LayoutManager. So, when you want to change the layout manager for the components you added to a JRootPane, be sure to use code like this:
    rootPane.getContentPane().setLayout(new BoxLayout());
 
If a JMenuBar component is set on the JRootPane, it is positioned along the upper edge of the frame. The contentPane is adjusted in location and size to fill the remaining area. (The JMenuBar and the contentPane are added to the layeredPane component at the JLayeredPane.FRAME_CONTENT_LAYER layer.)

The layeredPane is the parent of all children in the JRootPane -- both as the direct parent of the menu and the grandparent of all components added to the contentPane. It is an instance of JLayeredPane, 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.

The glassPane sits on top of all other components in the JRootPane. 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 use setVisible on the glassPane to control when the glassPane displays over the other children. By default the glassPane is not visible.

The custom LayoutManager used by JRootPane ensures that:

  1. The glassPane fills the entire viewable area of the JRootPane (bounds - insets).
  2. The layeredPane fills the entire viewable area of the JRootPane. (bounds - insets)
  3. The menuBar is positioned at the upper edge of the layeredPane.
  4. The contentPane fills the entire viewable area, minus the menuBar, if present.
Any other views in the JRootPane view hierarchy are ignored.

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

The painting architecture of Swing requires an opaque JComponent to exist in the containment hieararchy 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 of setOpaque(true). Additionally, if the content pane overrides paintComponent, it will need to completely fill in the background in an opaque color in paintComponent.

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 JavaBeansTM has been added to the java.beans package. Please see java.beans.XMLEncoder.

See Also:
JLayeredPane, JMenuBar, JInternalFrame, JComponent, BoxLayout, Mixing Heavy and Light Components

Nested Class Summary
protected  class JRootPane.RootLayout
          A custom layout manager that is responsible for the layout of layeredPane, glassPane, and menuBar.
 
Field Summary
protected  java.awt.Container contentPane
          The content pane.
protected  JButton defaultButton
          The button that gets activated when the pane has the focus and a UI-specific action like pressing the Enter key occurs.
static int ERROR_DIALOG
          Constant used for the windowDecorationStyle property.
static int FRAME
          Constant used for the windowDecorationStyle property.
protected  java.awt.Component glassPane
          The glass pane that overlays the menu bar and content pane, so it can intercept mouse movements and such.
static int INFORMATION_DIALOG
          Constant used for the windowDecorationStyle property.
protected  JLayeredPane layeredPane
          The layered pane that manages the menu bar and content pane.
protected  JMenuBar menuBar
          The menu bar.
static int NONE
          Constant used for the windowDecorationStyle property.
static int PLAIN_DIALOG
          Constant used for the windowDecorationStyle property.
static int QUESTION_DIALOG
          Constant used for the windowDecorationStyle property.
static int WARNING_DIALOG
          Constant used for the windowDecorationStyle property.
 
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JRootPane()
          Creates a JRootPane, setting up its glassPane, layeredPane, and contentPane.
 
Method Summary
protected  void addImpl(java.awt.Component comp, java.lang.Object constraints, int index)
          Overridden to enforce the position of the glass component as the zero child.
 void addNotify()
          Register ourselves with the SystemEventQueueUtils as a new root pane.
protected  java.awt.Container createContentPane()
          Called by the constructor methods to create the default contentPane.
protected  java.awt.Component createGlassPane()
          Called by the constructor methods to create the default glassPane.
protected  JLayeredPane createLayeredPane()
          Called by the constructor methods to create the default layeredPane.
protected  java.awt.LayoutManager createRootLayout()
          Called by the constructor methods to create the default layoutManager.
 java.awt.Container getContentPane()
          Returns the content pane -- the container that holds the components parented by the root pane.
 JButton getDefaultButton()
          Returns the value of the defaultButton property.
 java.awt.Component getGlassPane()
          Returns the current glass pane for this JRootPane.
 JMenuBar getJMenuBar()
          Returns the menu bar from the layered pane.
 JLayeredPane getLayeredPane()
          Gets the layered pane used by the root pane.
 int getWindowDecorationStyle()
          Returns a constant identifying the type of Window decorations the JRootPane is providing.
 boolean isOptimizedDrawingEnabled()
          The glassPane and contentPane have the same bounds, which means JRootPane does not tiles its children and this should return false.
 boolean isValidateRoot()
          If a descendant of this JRootPane calls revalidate, validate from here on down.
 void paint(java.awt.Graphics g)
          Invoked by Swing to draw components.
protected  void paintBorder(java.awt.Graphics g)
          Paints the component's border.
protected  void paintComponent(java.awt.Graphics g)
          Calls the UI delegate's paint method, if the UI delegate is non-null.
 void paintComponents(java.awt.Graphics g)
           
protected  java.lang.String paramString()
          Returns a string representation of this JRootPane.
protected  void printBorder(java.awt.Graphics g)
          Prints the component's border.
protected  void printComponent(java.awt.Graphics g)
          This is invoked during a printing operation.
 void printComponents(java.awt.Graphics g)
           
 void removeNotify()
          Unregister ourselves from SystemEventQueueUtils.
 void setContentPane(java.awt.Container content)
          Sets the content pane -- the container that holds the components parented by the root pane.
 void setDefaultButton(JButton defaultButton)
          Sets the defaultButton property, which determines the current default button for this JRootPane.
 void setGlassPane(java.awt.Component glass)
          Sets a specified Component to be the glass pane for this root pane.
 void setJMenuBar(JMenuBar menu)
          Adds or changes the menu bar used in the layered pane.
 void setLayeredPane(JLayeredPane layered)
          Sets the layered pane for the root pane.
 void setWindowDecorationStyle(int windowDecorationStyle)
          Sets the type of Window decorations (such as borders, widgets for closing a Window, title ...) the JRootPane should provide.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getSize, getToolTipText, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, isDoubleBuffered, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, paintChildren, paintImmediately, paintImmediately, print, printAll, printChildren, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, areFocusTraversalKeysSet, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, list, list, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getCursor, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getSize, getToolkit, getTreeLock, hasFocus, imageUpdate, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, list, list, list, paintAll, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, setBounds, setBounds, setCursor, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NONE

public static final int NONE
Constant used for the windowDecorationStyle property. Indicates that the JRootPane should not provide any sort of Window decorations.

Since:
1.4
See Also:
Constant Field Values

FRAME

public static final int FRAME
Constant used for the windowDecorationStyle property. Indicates that the JRootPane should provide decorations appropriate for a Frame.

Since:
1.4
See Also:
Constant Field Values

PLAIN_DIALOG

public static final int PLAIN_DIALOG
Constant used for the windowDecorationStyle property. Indicates that the JRootPane should provide decorations appropriate for a Dialog.

Since:
1.4
See Also:
Constant Field Values

INFORMATION_DIALOG

public static final int INFORMATION_DIALOG
Constant used for the windowDecorationStyle property. Indicates that the JRootPane should provide decorations appropriate for a Dialog used to display an informational message.

Since:
1.4
See Also:
Constant Field Values

ERROR_DIALOG

public static final int ERROR_DIALOG
Constant used for the windowDecorationStyle property. Indicates that the JRootPane should provide decorations appropriate for a Dialog used to display an error message.

Since:
1.4
See Also:
Constant Field Values

QUESTION_DIALOG

public static final int QUESTION_DIALOG
Constant used for the windowDecorationStyle property. Indicates that the JRootPane should provide decorations appropriate for a Dialog used to present a question to the user.

Since:
1.4
See Also:
Constant Field Values

WARNING_DIALOG

public static final int WARNING_DIALOG
Constant used for the windowDecorationStyle property. Indicates that the JRootPane should provide decorations appropriate for a Dialog used to display a warning message.

Since:
1.4
See Also:
Constant Field Values

menuBar

protected JMenuBar menuBar
The menu bar.


contentPane

protected java.awt.Container contentPane
The content pane.


layeredPane

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


glassPane

protected java.awt.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 the Enter key occurs.

Constructor Detail

JRootPane

public JRootPane()
Creates a JRootPane, setting up its glassPane, layeredPane, and contentPane.

Method Detail

getWindowDecorationStyle

public int getWindowDecorationStyle()
Returns a constant identifying the type of Window decorations the JRootPane is providing.

Returns:
One of NONE, FRAME, PLAIN_DIALOG, INFORMATION_DIALOG, ERROR_DIALOG, QUESTION_DIALOG or WARNING_DIALOG.
Since:
1.4
See Also:
setWindowDecorationStyle(int)

setWindowDecorationStyle

public void setWindowDecorationStyle(int windowDecorationStyle)
Sets the type of Window decorations (such as borders, widgets for closing a Window, title ...) the JRootPane 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:
java.lang.IllegalArgumentException - if style is not one of: NONE, FRAME, PLAIN_DIALOG, INFORMATION_DIALOG, ERROR_DIALOG, QUESTION_DIALOG, or WARNING_DIALOG.
Since:
1.4

paint

public final void paint(java.awt.Graphics g)
Description copied from class: JComponent
Invoked by Swing to draw components. Applications should not invoke paint directly, but should instead use the repaint method to schedule the component for redrawing.

This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. They're called in the order listed to ensure that children appear on top of component itself. Generally speaking, the component and its children should not paint in the insets area allocated to the border. Subclasses can just override this method, as always.

Overrides:
paint in class JComponent
Parameters:
g - the Graphics context in which to paint
See Also:
JComponent.paintBorder(java.awt.Graphics), JComponent.paintChildren(java.awt.Graphics), JComponent.getComponentGraphics(java.awt.Graphics), JComponent.repaint(long, int, int, int, int)

createLayeredPane

protected JLayeredPane createLayeredPane()
Called by the constructor methods to create the default layeredPane. Bt default it creates a new JLayeredPane.

Returns:
the default layeredPane

createContentPane

protected java.awt.Container createContentPane()
Called by the constructor methods to create the default contentPane. By default this method creates a new JComponent add sets a BorderLayout as its LayoutManager.

Returns:
the default contentPane

createGlassPane

protected java.awt.Component createGlassPane()
Called by the constructor methods to create the default glassPane. By default this method creates a new JComponent with visibility set to false.

Returns:
the default glassPane

createRootLayout

protected java.awt.LayoutManager createRootLayout()
Called by the constructor methods to create the default layoutManager.

Returns:
the default layoutManager.

setJMenuBar

public void setJMenuBar(JMenuBar menu)
Adds or changes the menu bar used in the layered pane.

Parameters:
menu - the JMenuBar to add

getJMenuBar

public JMenuBar getJMenuBar()
Returns the menu bar from the layered pane.

Returns:
the JMenuBar used in the pane

setContentPane

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

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

Parameters:
content - the Container to use for component-contents
Throws:
java.awt.IllegalComponentStateException - (a runtime exception) if the content pane parameter is null

getContentPane

public java.awt.Container getContentPane()
Returns the content pane -- the container that holds the components parented by the root pane.

Returns:
the Container 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 optional JMenuBar.

Parameters:
layered - the JLayeredPane to use
Throws:
java.awt.IllegalComponentStateException - (a runtime exception) if the layered pane parameter is null

getLayeredPane

public JLayeredPane getLayeredPane()
Gets the layered pane used by the root pane. The layered pane typically holds a content pane and an optional JMenuBar.

Returns:
the JLayeredPane currently in use

setGlassPane

public void setGlassPane(java.awt.Component glass)
Sets a specified Component 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. For example, only one JInternalFrame is ever active when using a DefaultDesktop, and any inactive JInternalFrames' glass panes are made visible so that clicking anywhere within an inactive JInternalFrame can activate it.

Parameters:
glass - the Component to use as the glass pane for this JRootPane
Throws:
java.lang.NullPointerException - if the glass parameter is null

getGlassPane

public java.awt.Component getGlassPane()
Returns the current glass pane for this JRootPane.

Returns:
the current glass pane
See Also:
setGlassPane(java.awt.Component)

isValidateRoot

public boolean isValidateRoot()
If a descendant of this JRootPane calls revalidate, validate from here on down.

Deferred requests to layout a component and its descendents again. For example, calls to revalidate, are pushed upwards to either a JRootPane or a JScrollPane because both classes override isValidateRoot to return true.

Overrides:
isValidateRoot in class JComponent
Returns:
true
See Also:
JComponent.isValidateRoot()

isOptimizedDrawingEnabled

public boolean isOptimizedDrawingEnabled()
The glassPane and contentPane have the same bounds, which means JRootPane does not tiles its children and this should return false. On the other hand, the glassPane is normally not visible, and so this can return true if the glassPane isn't visible. Therefore, the return value here depends upon the visiblity of the glassPane.

Overrides:
isOptimizedDrawingEnabled in class JComponent
Returns:
true if this component's children don't overlap

addNotify

public void addNotify()
Register ourselves with the SystemEventQueueUtils as a new root pane.

Overrides:
addNotify in class JComponent
See Also:
JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)

removeNotify

public void removeNotify()
Unregister ourselves from SystemEventQueueUtils.

Overrides:
removeNotify in class JComponent
See Also:
addNotify()

setDefaultButton

public void setDefaultButton(JButton defaultButton)
Sets the defaultButton property, which determines the current default button for this JRootPane. The default button is the button which will be activated when a UI-defined activation event (typically the Enter 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 a JTextPane). 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 to null.

Parameters:
defaultButton - the JButton which is to be the default button
See Also:
JButton.isDefaultButton()

getDefaultButton

public JButton getDefaultButton()
Returns the value of the defaultButton property.

Returns:
the JButton which is currently the default button
See Also:
setDefaultButton(javax.swing.JButton)

addImpl

protected void addImpl(java.awt.Component comp,
                       java.lang.Object constraints,
                       int index)
Overridden to enforce the position of the glass component as the zero child.

Parameters:
comp - the component to be enhanced
constraints - the constraints to be respected
index - the index

paramString

protected java.lang.String paramString()
Returns a string representation of this JRootPane. 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 be null.

Overrides:
paramString in class JComponent
Returns:
a string representation of this JRootPane.

paintComponent

protected final void paintComponent(java.awt.Graphics g)
Description copied from class: JComponent
Calls the UI delegate's paint method, if the UI delegate is non-null. We pass the delegate a copy of the Graphics object to protect the rest of the paint code from irrevocable changes (for example, Graphics.translate).

Overrides:
paintComponent in class JComponent
Parameters:
g - the Graphics object to protect
See Also:
JComponent.paint(java.awt.Graphics), ComponentUI

paintBorder

protected final void paintBorder(java.awt.Graphics g)
Description copied from class: JComponent
Paints the component's border.

Overrides:
paintBorder in class JComponent
Parameters:
g - the Graphics context in which to paint
See Also:
JComponent.paint(java.awt.Graphics), JComponent.setBorder(javax.swing.border.Border)

paintComponents

public final void paintComponents(java.awt.Graphics g)

printComponent

protected final void printComponent(java.awt.Graphics g)
Description copied from class: JComponent
This is invoked during a printing operation. This is implemented to invoke paintComponent on the component.

Overrides:
printComponent in class JComponent
Parameters:
g - the Graphics context in which to paint
See Also:
JComponent.print(java.awt.Graphics)

printBorder

protected final void printBorder(java.awt.Graphics g)
Description copied from class: JComponent
Prints the component's border. This is implemented to invoke paintBorder on the component.

Overrides:
printBorder in class JComponent
Parameters:
g - the Graphics context in which to paint
See Also:
JComponent.print(java.awt.Graphics)

printComponents

public final void printComponents(java.awt.Graphics g)

JSR-209 (Final Release)

Java and Java 2D are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
Copyright 1993 - 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All Rights Reserved.
Use of this specification is subject to this license.