Open In App

Java JRootPane

Last Updated : 23 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Java Swing, JRootPane is a fundamental component that serves as the root container for creating complex user interfaces. It encapsulates the core functionality of a top-level window, like JFrame, and provides a structure for organizing the content within a GUI application. In Java JRootPane is a part of javax.swing package. It does not contain any constructor it has only the default constructor.

Default constructor of this class

JRootPane(): It creates a JRootPane(), It setting up it’s layeredPane, contentPane and glassPane

Most used Methods of this class

Methods

Description

setContentPane(Container content)

This method allows you to set the content pane of the JRootPane.

setJMenuBar(JMenuBar menu)

Used to set the menu bar for the JRootPane.

setGlassPane(Component glass)

You can use this method to set the glass pane, which is typically an invisible layer used for custom painting, event handling, or overlaying the entire root pane.

getLayeredPane()

Returns the JLayeredPane that allows you to work with multiple layers within the root pane.

getContentPane()

Returns the content pane currently set in the JRootPane.

getJMenuBar()

Retrieves the menu bar associated with the JRootPane.

getGlassPane()

Retrieves the current glass pane.

setDefaultButton(JButton defaultButton)

Sets the default button for the root pane, which is the button that responds to the “Enter” keypress.

getDefaultButton()

Retrieves the default button currently set in the root pane.

Following are the programs to implement JRootPane

Example 1:

Java




// Java Program to Implement a JRootPane including creating
// a JFrame with a content pane, a menu bar, a glass pane,
// and a layered pane
import javax.swing.*;
  
public class RootPaneExample {
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run()
            {
                JFrame frame
                    = new JFrame("JRootPane Example");
                JRootPane rootPane = frame.getRootPane();
  
                // Step 1: Create a content pane
                JPanel contentPane = new JPanel();
                contentPane.add(
                    new JLabel("GeeksforGeeks"));
  
                // Step 2: Create a menu bar
                JMenuBar menuBar = new JMenuBar();
                JMenu fileMenu = new JMenu("File");
                JMenuItem openItem = new JMenuItem("Open");
                fileMenu.add(openItem);
                menuBar.add(fileMenu);
  
                // Step 3: Create and add a glass pane
                JPanel glassPane = new JPanel();
  
                // Make it transparent
                glassPane.setOpaque(false);
                  
              // Step 4: Create a layered pane and add
                // components
                JLayeredPane layeredPane
                    = rootPane.getLayeredPane();
                JLabel label1 = new JLabel("Label 1");
                JLabel label2 = new JLabel("Label 2");
                layeredPane.add(label1,
                                JLayeredPane.DEFAULT_LAYER);
                layeredPane.add(label2,
                                JLayeredPane.PALETTE_LAYER);
  
                // Set the content pane, menu bar, and glass
                // pane
                rootPane.setContentPane(contentPane);
                rootPane.setJMenuBar(menuBar);
                rootPane.setGlassPane(glassPane);
  
                frame.setSize(300, 200);
                frame.setDefaultCloseOperation(
                    JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}


Output:

Java JRootPane

Example 2:

Java




// Java Program to Implement setContentPane and setJMenuBar
// methods with JRootPane
import javax.swing.*;
  
// Driver Class
public class JRootPaneExample {
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> {
            // Create a JFrame
            JFrame frame = new JFrame("JRootPane Example");
            JRootPane rootPane = frame.getRootPane();
  
            // Step 1: Create a content pane and set it
            JPanel contentPane = new JPanel();
            JLabel label = new JLabel("Welcome to GFG");
            contentPane.add(label);
            rootPane.setContentPane(contentPane);
  
            // Step 2: Create a menu bar and set it
            JMenuBar menuBar = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
            JMenuItem openItem = new JMenuItem("Open");
            JMenuItem exitItem = new JMenuItem("Exit");
            fileMenu.add(openItem);
            fileMenu.add(exitItem);
            menuBar.add(fileMenu);
            rootPane.setJMenuBar(menuBar);
  
            // Make the frame visible and configure it
            frame.setSize(400, 200);
            frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}


Output:

Java JRootPane



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads