setContentPane(pane)

J

Jenny

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.

Thank you.

Jenny
 
D

David Hilsee

Jenny said:
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.
 
C

Cid

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

Often people set up their gui by adding elements to a panel and then
set that as the content of a JFrame. That's a good scenario for
setContentPane() (a desired panel already exists).
 
D

David Hilsee

Cid said:
...

Often people set up their gui by adding elements to a panel and then
set that as the content of a JFrame. That's a good scenario for
setContentPane() (a desired panel already exists).

To each his own, I suppose. In the end, the results are the same, so I
guess the difference is a matter of personal preference.
 
J

Jenny

Liz said:
I don't see what is a ClockPanel();


Jenny said:
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.

Thank you.

Jenny


You can see it follow the link
http://www.cadenhead.org/book/java24hours/source/chapter13
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Create new class vs extends a class 1
revise my code 2
JFrame window dimensions 7
problem_in_my_code 2
Play .wav music file 13
revise this code 0
Image doesn't display properly 4
Draw a string on JPanel 1

Members online

No members online now.

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,353
Latest member
RogerDoger

Latest Threads

Top