What is the difference between Java frame functions getContentPane() and getRootPane()?
Also what wil happen when we set a JButton as Default.
from documentation:
getContentPane() is generally implemented like this:
public Container getContentPane() {
return getRootPane().getContentPane();
}
It's well described in Swing tutorial (here).
While using top-level containers in AWT or Swing, the root pane is the base pane.
The hierarchy is as follows:
Glass Pane: Generally hidden, setting to visible will show up a glass cover over the root pane areas.
Layered Pane: Contains the Menubar and the Content Pane
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?
The Root Pane, as the name implies, is the root of the frame/window/dialog.
Its contains als other components of this top-level component.
the content pane is one of the four parts of the root pane and contains the components. the other parts of the root pane are glass pane, layered pane and a optional menu bar.
the tutorials at oracle explains this really good:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
Related
Why JMenuBar is not place in the content pane?is there any reason or effects when make a java gui program especially when using jframe?
Thanks
As stated in Using Top-Level Containers article, the manu bar is managed by the Root Pane:
Each top-level container relies on a reclusive intermediate container
called the root pane. The root pane manages the content pane and the
menu bar, along with a couple of other containers. You generally don't
need to know about root panes to use Swing components. However, if you
ever need to intercept mouse clicks or paint over multiple components,
you should get acquainted with root panes.
A menu bar is tipically placed at the top of a window, so if it would be placed in the content pane then the Layout managers would have to deal with this particular component. By segregating the menu bar from the content pane then the responsibility of laying out the menu bar and the rest of components is well divided. In addition, the menu bar location is fixed and this component is not dragabble so this can be laid out in a different way than the rest of components.
About the JToolBar this component is draggable and you may have several of them, placing one above another one and rearranging their order as you wish. So this component is totally different of a menu bar and makes sense it be placed in the content pane just as a text field or another regular component.
Suggested readings:
Using Top-Level Containers
How to Use Root Panes
How to Use Menus
Can anybody tell me what is the difference between the below Code
JFrame jf=new JFrame();
JButton jb=new JButton();
jf.add(jb);
and
JFrame jf=new JFrame();
Container c=jf.getContentPane();
JButton jb=new JButton();
c.add(jb);
Even I am not Clear about the RootPane,LayeredPane,GlassPane. What is the use of RootPane?? I have never used it in my coding. I have read it from the below link
http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html
but not too much clear because there is no practical situation where I have used the above.
Thanks for your answer!!
As of Java 5, there is no difference between you code examples. JFrame#add is now redirected to JFrame#getContentPane on your behalf.
Before Java 5, it would throw an exception, which meant you had to add your components directly to the content pane yourself.
The JRootPane is used to provide a light weight container onto which other Swing components can be added. More importantly, it provides a means by which the top level Swing containers can organise themselves.
The JRootPane is made of layers. At the bottom is the JMenuBar and content pane and above that is the glass pane.
Because of the way it's constructed, the JRootPane can actually have a number of additional layers (typically between the content pane and the glass pane), this is typically used for things like popups.
The glass pane acts as a overlay which can be used to render content over the top of everything else (as well as block mouse and keyboard events).
Take a look at How to use Root Panes.
Normally, other then the content pane and glass pane, you wouldn't typically use any of the other parts of the root pane.
You could also take a look at this for a quick example of a glass pane in use
Both are same...
Both are added in java.awt.Container object.
JFrame and Container have following hierarchy
Case 1: JFrame#add()--> Frame#add()--> Window#add()--> Container#add() // Component add to container
Case 2: JFrame#getContentPane() --> getRootPane().getContentPane(); it will return Container object(Container#add)
Anyone explain to me the difference between content pane and layout with by example. Give code example where exactly need of content pane (or) layout.
What is the difference between these, and in what circumstances should you use one rather than the others?
There is no comparison between content pane and layout; they are two distinct entities.
Content Pane :
The default content pane is a simple intermediate container that inherits from JComponent, and that uses a BorderLayout as its layout manager.
Layout
It is used to place components on the parent container. These are basically the set of constraints, which establish the position of a given component on the parent container.
They both are used as you make a Swing Application. As when you write
frame.add(childComponent);
the childComponent is actually added to the content pane. Three methods, add(...), remove(...) and setLayout(...), are overridden for content pane.
I found three ways to fill my JFrame frame = new JFrame("...")
createContentPanel returns a JPanel and createToolBar returns a ToolBar.
frame.add(this.createToolBar(), BorderLayout.PAGE_START); //this works and puts the ToolBar above and the ContentPanel under it<br>
frame.add(this.createContentPanel(), BorderLayout.CENTER);
frame.setContentPane(this.createContentPanel()); //this lets the JToolBar hover over the ContentPanel
frame.getContentPane().add(this.createToolBar());
frame.getContentPane().add(this.createContentPanel()); //this only puts the last one into the JFrame
frame.getContentPane().add(this.createToolBar());
And now I am wondering why should i use the getContentPane()/setContentPane() method if i could just use a simple frame.add(...) to fill my frame.
You are right that it doesn't matter which you use (JFrame#add(...) vs. JFrame#getContentPane().add(...)) since they both essentially call the same code, however there will be times in the future when you'll need access to the contentPane itself, such as if you want to change its border, set its background color or determine its dimensions, and so you'll likely use getContentPane() at some point, and thus getting to know it and be familiar with it would be helpful.
//this only puts the last one into the JFrame
You need to understand how layout managers work. The default content pane is a JPanel that uses a BorderLayout. When you add a component and don't specify a constraint, then it defaults to the CENTER. However you can only has a single component in the center so the layout manager only knows about the last one added. When the layout manager is invoked it sets the size() and location() of that component. The other component has a size of 0, so it is never painted.
In Java 1.6, you can just use the add method of JFrame:
http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html
(It will be delegated to the contentPane.)
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html
Which says:
The JFrame class is slightly
incompatible with Frame. Like all
other JFC/Swing top-level containers,
a JFrame contains a JRootPane as its
only child. The content pane provided
by the root pane should, as a rule,
contain all the non-menu components
displayed by the JFrame. This is
different from the AWT Frame case. For
example, to add a child to an AWT
frame you'd write:
frame.add(child);
However using JFrame you need to add the child
to the JFrame's content pane instead:
frame.getContentPane().add(child);
The same is true for setting layout
managers, removing components, listing
children, and so on. All these methods
should normally be sent to the content
pane instead of the JFrame itself. The
content pane will always be non-null.
Attempting to set it to null will
cause the JFrame to throw an
exception. The default content pane
will have a BorderLayout manager set
on it.
I've always been a little fuzzy on the difference between the glass pane and a layered pane. Is the glass pane essentially just "the very top layer of the root pane," or does it behave differently? When would you use a layered pane instead of the glass pane?
They are two different things:
the layered pane is the destination of all the contents that are added or shown inside a JFrame. For example, every normal component like JLabels, JTextFields, JTable etc.. in addition it implictly handles z-ordering of elements that are added to it so it can handles popup menus, or drag and drop effects: this because a popup menu is added to the layered pane with a z higher than the normal components, with the final effect to stay on top of other things. See here to understand the behaviour better.
the glass pane is an optional layer that is hidden by default and stays in any case on top of the layer pane. So basically everything you draw onto the glass pane will be always visible: you can think of it as a trasparent sheet that is applied on the top of a normal JFrame that you can choose to use you need special effects.
Both the layered pane and the glass pane are placed on the root pane that is the basis from which every frame is built up.