aspose file tools*
The moose likes Beginning Java and the fly likes difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference" Watch "difference" New topic
Author

difference

narayana satish
Greenhorn

Joined: Dec 21, 2000
Posts: 25
What is the basic difference between the JFrame and the JPanel and tell me the reason why we are getContentPane().add(panel); while using the JPanel. Then tell me when we have to use Container con=getContentPane(). expecting the gurus reply
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I believe that the difference is that JFrame is a top-level container, and thus has a native reference ( i.e. if you are running on windows, a JFrame has a reference to a native windows frame ) whereas JPanel is created completely with Java and is just drawn on the JFrame, thus having no native reference to OS specific components. ( This is different from AWT... all AWT components had native peers... ) I would assume that having to make the getContentPane() call to add things to a JFrame has something to do with this... ( I may be wrong... I am not as well versed in Swing as I would like to be... )
-Nate
P.S. - You may have better luck in the Swing / AWT forum...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Fred Hosch
Author
Greenhorn

Joined: Apr 16, 2001
Posts: 23
yes, JFrame is-a AWT Frame -- a heavyweight component that has
a "peer" in the native windowing system, with all the overhead
that implies.
JFrames have lots of structure, of which the content pane is
only one piece. A classic case of paying the price of complexity
for flexibility.
(Remember this is a "2nd generation" design; lot's of the
"why'd they do that?" questions are answered "because the AWT
was already there, and they couldn't start with a virgin
sheet of paper.")
------------------
Fred Hosch
Author of:
An Introduction to Software Construction with Java


Fred Hosch<br />Author of:<br /><a href="http://www.amazon.com/exec/obidos/ASIN/0471354899/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">An Introduction to Software Construction with Java</a>
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Originally posted by narayana satish:
What is the basic difference between the JFrame and the JPanel and tell me the reason why we are getContentPane().add(panel); while using the JPanel. Then tell me when we have to use Container con=getContentPane(). expecting the gurus reply

As mentioned above, a JFrame is meant to be a top-level container, for holding other components (like JPanels). A JPanel, on the other hand, is used not only to hold other components, but also to arrange/organize components in virtually any way imaginable (via combinations of layout managers and parent panels/frames). Think of a JFrame as the big-daddy of JPanels, as it holds all the JPanels but is held itself by nothing. It is at the top of the GUI food chain.
As far as your content pane question:
The content pane is the part of a JFrame that is used just for adding components to. Anything you want your JFrame to contain MUST be added to the content pane. The two statements you used in your post -
getContentPane().add(panel);
and Container con=getContentPane(); will both work, but the first one would ideally be used when you only have one thing to add to the JFrame. The second one (with a few changes) is better when you have more than one thing to add. Once you have the reference to the content pane (with JPanel con=parentJFrame.getContentPane();, you could just say con.add(component); for every component you want to add. Note that you usually use a JPanel as the content pane for a JFrame, but there are other components you could use too.
I suggest going through Sun's tutorial on Swing to get a better understanding of GUI design.
------------------
  • Ryan Burgdorfer
  • Java Acolyte

  • [This message has been edited by ryan burgdorfer (edited April 20, 2001).]


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
 
jQuery in Action, 2nd edition
 
subject: difference