JFrames

Assuming I have one JPanel with some components, and I want that panel in a JFrame, what is the practical difference betweenframe.getContentPane().add(panel);andframe.setContentPane(panel);Thanx to one and all.
[231 byte] By [ValentineSmitha] at [2007-10-3 3:17:14]
# 1

Hi,

in the first approach you will have container (contentPane) and in this you will put another container (JPanel). In the second approach you will replace the default contentPane by your new contentPane (JPanel).

The structure can be seen in this tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html

L.P.

lukika at 2007-7-14 21:08:46 > top of Java-index,Desktop,Core GUI APIs...
# 2

First of all, my question still remains - is there any practical difference?

> In the second approach you will replace the default contentPane by your new contentPane (JPanel).

I wasn't aware that a JPanel was a top-level container with a content pane.

What am I missing?

ValentineSmitha at 2007-7-14 21:08:46 > top of Java-index,Desktop,Core GUI APIs...
# 3

In the first approach, you will be ADDING the panel to the current ConentPane as specified by your layout. In the second, you completely replace the panel.

Practically, this will differ if you 1) have some layout that displays both panels, and 2) the garbage collector will never destoy the current content pane which you would like to override as it is still containing active elements (i.e. the panel you added).

Message was edited by:

jeffus

jeffusa at 2007-7-14 21:08:46 > top of Java-index,Desktop,Core GUI APIs...
# 4
Well, maybe I'm dense, but, that was as clear as mud.Anyone else want to give it a try?
ValentineSmitha at 2007-7-14 21:08:46 > top of Java-index,Desktop,Core GUI APIs...
# 5

> is there any practical difference?

No, except maybe the LayoutManager. By default the JPanel of the content pane uses a BorderLayout. By default a JPanel uses a FlowLayout.

If you think you need to access the content pane as a JPanel then just create your own variable:

JPanel contentPane = (JPanel)frame.getContentPane().

camickra at 2007-7-14 21:08:46 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanx camickr.
ValentineSmitha at 2007-7-14 21:08:46 > top of Java-index,Desktop,Core GUI APIs...