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
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...
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
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>
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. ------------------
[This message has been edited by ryan burgdorfer (edited April 20, 2001).]