Hi Mike
You must know that Java Swing is like Kung Fu: it's more than a sport,
it's a philosophy. And before being able to fully taking advantages of
its benefits one must learn how to raise bonsais, carry the water and
wash the windows for at least a couple of years :-)
Now, as far as I know (I must confess I have skipped some windows
washing classes, not to speak about my bonsais), a JFrame is the parent
of several embedded containers that allow fancy effects:
-- The RootPane (use myFrame.getRootPane() to grab it) is a JRootPane
(derived from Component); its role is to contain the GlassPane and the
LayeredPane
-- The LayeredPane (use myFrame.getLayeredPane() or
myFrame.getRootPane.getLayeredPane()) is a JLayeredPane derived from
JComponent derived from Container derived from Component; it contains
several graphical "layers" (in the z-Order) with specific functions; it
is the container for the frame menu and for the ContentPane
-- The GlassPane (use myFrame.getGlassPane() or
myFrame.getRootPane.getGlassPane() or
myFrame.getRootPane.getLayeredPane.getGlassPane() ...) is an AWT
Component (or some derived class); it is a transparent layer above the
Frame contents and allows to add some fancy effects, such as obscuring
the whole Frame, to display additional fancy messages or controls and to
intercept user input (for e.g., to display an input aid while typing)
-- The ContentPane (myFrame.getContentPane() or ...) is an AWT Container
(or some derived class - in practice it is a JPanel, but you can replace
it by any AWT Container); it displays (or draws if you prefer) the
Components of the Frame.
The description of whole structure can be found in the Sun tutorials
about Swing (I don't have the URL under my hand) but the role of each
component is not too explicit.
You can (1) directly add the JFrame Components to the ContentPane
(including specifying its LayoutManager). In fact, the "add" methods of
the JFrame are overridden so they call the corresponding "add" methods
of the ContentPane (same for the LayoutManager management methods).
Or you can (2) create your own Container, such as a JPanel, add
Components to it and replace the ContentPane of your JFrame.
Or you can (3) use your own JPanel to add Components and play with the
LayoutManager and, in the end, add it to the (ContentPane of the) JFrame.
The simplest ways is to use the first method. However some people prefer
the methods 2 and 3 for some reasons and consider doing so as a "best
practice".
One of the reason is that you can work to your JPanel and all the logic
behind without asking yourself about the final enclosing container. Then
decide (eventually at execution time) to display it in a JFrame (and let
the user play with several things on the screen including your JPanel)
or in a modal JDialog (so to prevent the user to touch anything else as
long as your JPanel is in sight) or in a JApplet (because the user
requested your JPanel over the Internet or the company Intranet).
I think that another (historical) reason was caused by the fact that
displaying Swing controls used to be very slow on 32MB Windows 95
systems (or so). So it was a common practice to display some splash
screen while constructing the content of the JPane then had quickly
displayed the whole when the JPane was added to the Frame.
The last but not the least reason is the ability to quickly change the
content of the JFrame. Just have two contextual JPanes ready to be
displayed and replace the ContentPane with the one that fits your
context at that moment. Then change it again when the context changes.
For other fancy effects one can produce by taking advantage of the
JFrame layered structure, I found the O'Reilly's book "Swing Hacks -
Tips and Tools for Building Killer GUIs", by Marinacci & Adamson. It
implies some windows washing and meditation too so I cannot say I am an
expert in killer GUIs based applications, but it worth to take a look at it.
Hope it helps
Mihai
Mike M. Lin a écrit :
Thanks for the link, Michčle. The interesting thing is that I didn't
need to the create a JPanel to do my layout. Immediately after
creating my JFrame, I just set the layout directly on it.
JFrame frame = new JFrame("It's time to play SHOW THAT FLAG");
frame.setLayout(new BoxLayout(
frame.getContentPane(), BoxLayout.PAGE_AXIS));
This article,
http://weblogs.java.net/blog/hansmuller/archive/2005/11/jframeadd_conte.html,
explains that this is actually an illusion -- a convenience feature
that actually sets the layout on the content pane of the frame. The
above is equivalent to this:
JFrame frame = new JFrame("It's time to play SHOW THAT FLAG");
frame.getContentPane().setLayout(new BoxLayout(
frame.getContentPane(), BoxLayout.PAGE_AXIS));
Even more interesting is that I never set the content pane of the
frame before setting the content pane's layout. How can I set the
layout on an object that doesn't exist? What I found out is that
every JFrame comes with a default content pane. What type of object
is it? Well let's find out...
public static void main(String[] args) {
JFrame frame = new JFrame();
System.out.println("frame.getContentPane() is a "
+ frame.getContentPane().getClass().getName());
}
run:
frame.getContentPane() is a javax.swing.JPanel
BUILD SUCCESSFUL (total time: 2 seconds)
It's actually a JPanel. So if the JFrame already includes a default
content pane that's a JPanel, then why create another JPanel to slap
on top of it? In the LunarPhases example (http://137.166.68.100/java-
tut/uiswing/learn/example5.html), they could have done without the
mainPanel and instead added the selectPanel and displayPanel to the
lunarPhasesFrame's content pane.
One possible advantage that I could see is that the mainPanel is used
to throw a border around the edges of the frame so that the
selectPanel and displayPanel don't seem so cramped. If that's what
the purpose was, then it wasn't clear to me from reading the tutorial.
Thanks,
Mike
On Jul 21, 2:21 am, Michčle Garoche <[email protected]> wrote:
On 21 juil, 10:16, "Mike M. Lin" <[email protected]> wrote:> In the Swing
homework, the example we key off of (lunar images) is set
up with a JFrame which holds a JPanel. that JPanel acts as a container
for two other JPanel's - one to house the combo box, the other to
house the image.
What is the purpose of the containing JPanel? I did my homework
assignment without that container and it seems to work just fine. I
just put my sub-panels directly on the JFrame.
It seems pretty useless to me in this program. What am I missing?
You may have a look at this
tutorial:http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/...
Especially the section Tips on choosing a Layout Manager.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en