In general, you don't directly create a JRootPane
object. Instead, you get a JRootPane
(whether you want it or not!) when you instantiate JInternalFrame
or one of the top-level Swing containers, such as JApplet
, JDialog
, and JFrame
.
Using Top-Level Containers tells you the basics of using root panes — getting the content pane, setting its layout manager, and adding Swing components to it. This section tells you more about root panes, including the components that make up a root pane and how you can use them.
As the preceding figure shows, a root pane has four parts:
paintComponent
method so that it does something, and it can intercept input events for the root pane. In the next section, you'll see an example of using a glass pane.
setJMenuBar
method to put the menu bar in the appropriate place. For more information on using menus and menu bars, see How to Use Menus.
The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components. For example, you can deactivate mouse events for a multi-component region by having the glass pane intercept the events. Or you can display an image over multiple components using the glass pane.
Here's a picture of an application that demonstrates glass pane features. It contains a check box that lets you set whether the glass pane is "visible" — whether it can get events and paint itself onscreen. When the glass pane is visible, it blocks all input events from reaching the components in the content pane. It also paints a red dot in the place where it last detected a mouse-pressed event.
Try this:The following code from GlassPaneDemo.java
shows and hides the glass pane. This program happens to create its own glass pane. However, if a glass pane doesn't do any painting, the program might simply attach listeners to the default glass pane, as returned by getGlassPane
.
myGlassPane = new MyGlassPane(...); changeButton.addItemListener(myGlassPane); frame.setGlassPane(myGlassPane); ... class MyGlassPane extends JComponent implements ItemListener { ... //React to change button clicks. public void itemStateChanged(ItemEvent e) { setVisible(e.getStateChange() == ItemEvent.SELECTED); } ... }
The next code snippet implements the mouse-event handling for the glass pane. If a mouse event occurs over the check box, then the glass pane redispatches the event so that the check box receives it.
...//In the implementation of the glass pane's mouse listener: public void mouseMoved(MouseEvent e) { redispatchMouseEvent(e, false); } .../* The mouseDragged, mouseClicked, mouseEntered, * mouseExited, and mousePressed methods have the same * implementation as mouseMoved. */... public void mouseReleased(MouseEvent e) { redispatchMouseEvent(e, true); } private void redispatchMouseEvent(MouseEvent e, boolean repaint) { Point glassPanePoint = e.getPoint(); Container container = contentPane; Point containerPoint = SwingUtilities.convertPoint( glassPane, glassPanePoint, contentPane); if (containerPoint.y < 0) { //we're not in the content pane //Could have special code to handle mouse events over //the menu bar or non-system window decorations, such as //the ones provided by the Java look and feel. } else { //The mouse event is probably over the content pane. //Find out exactly which component it's over. Component component = SwingUtilities.getDeepestComponentAt( container, containerPoint.x, containerPoint.y); if ((component != null) && (component.equals(liveButton))) { //Forward events over the check box. Point componentPoint = SwingUtilities.convertPoint( glassPane, glassPanePoint, component); component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger())); } } //Update the glass pane if requested. if (repaint) { glassPane.setPoint(glassPanePoint); glassPane.repaint(); } }
Here is the code in MyGlassPane
that implements the painting.
protected void paintComponent(Graphics g) { if (point != null) { g.setColor(Color.red); g.fillOval(point.x - 10, point.y - 10, 20, 20); } }The Layered Pane
A layered pane is a container with depth such that overlapping components can appear one on top of the other. General information about layered panes is in How to Use Layered Panes. This section discusses the particulars of how root panes use layered panes.
Each root pane places its menu bar and content pane in an instance of JLayeredPane
. The Z ordering that the layered pane provides enables behavior such as displaying popup menus above other components.
You can choose to put components in the root pane's layered pane. If you do, then you should be aware that certain depths are defined to be used for specific functions, and you should use the depths as intended. Otherwise, your components might not play well with the others. Here's a diagram that shows the functional layers and their relationship:
The table below describes the intended use for each layer and lists the JLayeredPane
constant that corresponds to each layer:
FRAME_CONTENT_LAYER
new Integer(-30000)
The root pane adds the menu bar and content pane to its layered pane at this depth. DEFAULT_LAYER
new Integer(0)
If you don't specify a component's depth when adding it to a layered pane, the layered pane puts it at this depth. PALETTE_LAYER
new Integer(100)
This layer is useful for floating tool bars and palettes. MODAL_LAYER
new Integer(200)
Modal internal-frame dialogs would belong in this layer. POPUP_LAYER
new Integer(300)
Popups go in this layer because they need to appear above just about everything. DRAG_LAYER
new Integer(400)
Intended to be used when a component is being dragged. The component should return to its regular layer when dropped.
Here is a picture of RootLayeredPaneDemo, which is a version of LayeredPaneDemo that uses the root pane's layered pane, rather than creating a new layered pane.
Try this:The tables that follow list the API for using root panes, glass panes, and content panes. For more information on using content panes, go to Using Top-Level Containers. Here are the tables in this section:
The API for using other parts of the root pane is described elsewhere:
Using a Root Pane Method Purpose JRootPane getRootPane()JApplet
, JDialog
, JFrame
, JInternalFrame
, and JWindow
) Get the root pane of the applet, dialog, frame, internal frame, or window. static JRootPane getRootPane(Component)
SwingUtilities
) If the component contains a root pane, return that root pane. Otherwise, return the root pane (if any) that contains the component. JRootPane getRootPane()
JComponent
) Invoke the SwingUtilities
getRootPane
method for the JComponent
. void setDefaultButton(JButton)
Every Swing program has a root pane, but few reference it directly. The examples in the following list illustrate how to use features of JRootPane
or the glass pane. Also see these lists:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4