Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between JFrame.getContentPane() and JFrame.getRootPane()?

What is the difference between Java frame functions getContentPane() and getRootPane()? Also what wil happen when we set a JButton as Default.

like image 905
Ajay Yadav Avatar asked Sep 09 '11 12:09

Ajay Yadav


People also ask

What is frame getContentPane ()?

The getContentPane() method retrieves the content pane layer so that you can add an object to it. The content pane is an object created by the Java run time environment. You do not have to know the name of the content pane to use it.

What is getRootPane () Java?

getRootPane() Return this component's single JRootPane child. void. setContentPane(Container contentPane) The "contentPane" is the primary container for application specific components.

What is difference between frame and JFrame?

JFrame is a top-level container that provides a window on the screen. A frame is actually a base window on which other components rely, namely the menu bar, panels, labels, text fields, buttons, etc. Almost every other Swing application starts with the JFrame window.

What is setDefaultCloseOperation JFrame Exit_on_close?

setDefaultCloseOperation() EXIT_ON_CLOSE — Exit the application. JFrame. HIDE_ON_CLOSE — Hide the frame, but keep the application running. JFrame. DISPOSE_ON_CLOSE — Dispose of the frame object, but keep the application running.


2 Answers

from documentation:

getContentPane() is generally implemented like this:

public Container getContentPane() {
    return getRootPane().getContentPane();
}

It's well described in Swing tutorial (here).

enter image description here

like image 93
Michał Šrajer Avatar answered Sep 27 '22 16:09

Michał Šrajer


While using top-level containers in AWT or Swing, the root pane is the base pane.

The hierarchy is as follows:

  1. Glass Pane: Generally hidden, setting to visible will show up a glass cover over the root pane areas.
  2. Layered Pane: Contains the Menubar and the Content Pane
  3. Content Pane: Is the basic layout pane in which components are actually placed.

Calling the method getRootPane() will return the reference to the base pane, while calling the getContentPane() method will get you the reference to the Content Pane. It is visible by default.

By setting Jbutton default, What are you exactly trying to accomplish?

like image 36
jagbandhuster Avatar answered Sep 27 '22 17:09

jagbandhuster