Re: setContentPane(pane)
From: David Hilsee (davidhilseenews_at_yahoo.com)
Date: 07/31/04
- Next message: Daniel Sjöblom: "Re: jni jint = int ?"
- Previous message: Roedy Green: "Re: Your Guru Paul Graham is getting trashed on Slashdot."
- Next in thread: Cid: "Re: setContentPane(pane)"
- Reply: Cid: "Re: setContentPane(pane)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 31 Jul 2004 02:01:59 -0400
"Jenny" <jenny_jones_79@hotmail.com> wrote in message
news:88d928c2.0407302055.11c7db7d@posting.google.com...
> Hi,
>
> In the link
http://www.cadenhead.org/book/java24hours/source/chapter13/ClockFrame.java
>
> it has this code:
>
> import java.awt.*;
> import javax.swing.*;
>
> public class ClockFrame extends JFrame {
> public ClockFrame() {
> super("Clock");
> setSize(225, 125);
> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> Container pane = getContentPane();
> FlowLayout flo = new FlowLayout();
> pane.setLayout(flo);
> ClockPanel time = new ClockPanel();
> pane.add(time);
> setContentPane(time);
> setVisible(true);
> }
>
> public static void main(String[] arguments) {
> ClockFrame sal = new ClockFrame();
> }
> }
>
> Is any difference if I use setContentPane(pane) instead of
> setContentPane(time)?
> They both work. But I do not understand what they do.
I have developed a few Swing applications, and I have never found it
necessary to call setContentPane() on any Swing GUI class. In every
application that I have developed, I chose to retrieve the content pane and
modify it (using getContentPane() and methods that are exposed by Container,
like setLayout()). IMHO, it would probably be best to avoid calling
setContentPane() because, IME, few applications require it, and, usually,
the same functionality can be achieved by modifying the existing pane. Most
of the time, it's just as good to specify a BorderLayout for the content
pane returned by getContentPane() with a single component whose layout
constraints are specified as BorderLayout.CENTER, and most Java programmers
will be able to follow that implementation. If you were to follow my
direction, in the above code, that would mean that ClockPanel would not be
set as the content pane for ClockFrame ("setContentPane(time)" could be
removed), and that call would instead specify a panel that is constrained by
the content pane's layout (probably BorderLayout) previously specified for
the content pane for ClockFrame (that is, getContentPane() is still called
but additional methods are invoked on the Container that was returned).
Again, my answer is mostly concerned with readability and not with what is
absolutely necessary.
-- David Hilsee
- Next message: Daniel Sjöblom: "Re: jni jint = int ?"
- Previous message: Roedy Green: "Re: Your Guru Paul Graham is getting trashed on Slashdot."
- Next in thread: Cid: "Re: setContentPane(pane)"
- Reply: Cid: "Re: setContentPane(pane)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]