Use case of Glass Pane vs. Layered Pane - java

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.

Related

Why JMenuBar is not place in the JFrame content pane, but JToolbar place in the content pane

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

How to drag components across panels in java

Is it possible to drag a JLabel from one panel to another?
I can only drag the label within one panel. When the label moves out of the burden, it just disappear. I don't know what to do.
You can easily do this in several ways. One way is to
bump the label to the glass pane on mousePressed(...)
drag it in the glass pane on mouseDragged(...),
then drop it down to whatever container the mouse is currently over on mouseReleased(...).
You will have to remove the label from its current container when you start dragging it. Normally we add it to the glass pane to allow for the illusion of been dragged across multiple components.
Take a look at My drag is better then yours for an example. I'd also recommend that you have a dig around his site, he does some nice work with drag and drop

Why there are too much layering in the Container in the swing?

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)

JScrollBar with highlighting position of selected text in JTextPane

I have JTextPane inside JScrollPane. When I highlight some word in text pane I want to have their position highlighted on JScrollBar (Similar to highlighting errors in source code in Eclipse).
Is this possible with Swing?
The "immediate" problem you have is the fact that the scroll pane does not support the concept of "row footers", which would provide you an area on the right hand side of the scroll pane you could render you highlight points
Choice #1
What you need is an implementation that does. You could take a look at JideScrollPane which provides not only support for row footers, but column footers as well.
From there it's a simple case of using the same concept as decorating a normal scroll pane (row and column header).
Check out Providing Custom Decorations for some hints.
Choice #2
The other choice (I can think of) would be to place a normal JScrollPane onto a JPanel using a BorderLayout so that the scroll pane occupied the center position. This would then allow you to place a custom component to the EAST position that would act as you "marker" pane.
This is slightly simpler, as it doesn't require a lot of additional changes to be made. You would then need to calculate the position of the text in the view port as a percentage of the it's height, which would allow you to translate it back to the "marker" pane

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.
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

Categories