Java JRootPane

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Java JRootPane

JRootPane is a lightweight container used behind the scenes by JFrame, JDialog,


JWindow, JApplet, and JInternalFrame.

Nested Classes

Modifier Class Description


and Type

protected JRootPane.AccessibleJRootPan This class implements accessibility support


class e for the JRootPane class.

protected JRootPane.RootLayout A custom layout manager that is responsible


class for the layout of layeredPane, glassPane, and
menuBar.

Fields

Modifier Field Description


and Type

static int COLOR_CHOOSER_DIALO Constant used for the windowDecorationStyle


G property.

protected contentPane The content pane.


JButton

protected defaultButton The button that gets activated when the pane
Container has the focus and a UI-specific action like
pressing the Enter key occurs.

protected menuBar The menu bar.


JMenuBar

protected glassPane The glass pane that overlays the menu bar and
Component content pane, so it can intercept mouse
movements and such.
static int ERROR_DIALOG Constant used for the windowDecorationStyle
property.

Constructor

Constructor Description

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

Useful Methods

Modifier and Method Description


Type

protected void addImpl(Component comp, Overridden to enforce the position of


Object constraints, int index) the glass component as the zero child.

void addNotify() Notifies this component that it now


has a parent component.

protected createContentPane() It is called by the constructor methods


Container to create the default contentPane.

protected createGlassPane() It called by the constructor methods


Component to create the default glassPane.

AccessibleContext getAccessibleContext() It gets the AccessibleContext


associated with this JRootPane.

JButton getDefaultButton() It returns the value of the


defaultButton property.

void setContentPane(Container It sets the content pane -- the


content) container that holds the components
parented by the root pane.

void setDefaultButton(JButton It sets the defaultButton property,


defaultButton) which determines the current default
button for this JRootPane.
void setJMenuBar(JMenuBar menu) It adds or changes the menu bar used
in the layered pane.

JRootPane Example
1. import javax.swing.JButton;  
2. import javax.swing.JFrame;  
3. import javax.swing.JMenu;  
4. import javax.swing.JMenuBar;  
5. import javax.swing.JRootPane;  
6.   
7. public class JRootPaneExample {  
8.      public static void main(String[] args) {  
9.             JFrame f = new JFrame();  
10.             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
11.             JRootPane root = f.getRootPane();  
12.   
13.             // Create a menu bar  
14.             JMenuBar bar = new JMenuBar();  
15.             JMenu menu = new JMenu("File");  
16.             bar.add(menu);  
17.             menu.add("Open");  
18.             menu.add("Close");  
19.             root.setJMenuBar(bar);  
20.   
21.             // Add a button to the content pane  
22.             root.getContentPane().add(new JButton("Press Me"));  
23.   
24.             // Display the UI  
25.             f.pack();  
26.             f.setVisible(true);  
27.           }  
28. }  
Output

You might also like