IntelliJ Java IDE
The moose likes Swing / AWT / SWT / JFace and the fly likes Difference when using getContentPane and when not Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "Difference when using getContentPane and when not" Watch "Difference when using getContentPane and when not" New topic
Author

Difference when using getContentPane and when not

deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 136
I wrote two program

In first i used getContentPane() to add a component E.g there is JPanel p , JButton b and a JFrame f so

p.add(b);
f.getContentPane().add(p)


and in second program i wrote only this

f.add(p);

I didnt find any difference...so what is the difference between the statements one with contentpane and one without contentpane...This question might pop up in the interview

Thanks in advance
Thomas Todaro
Greenhorn

Joined: Feb 02, 2012
Posts: 3
As long as you are running on 1.5 or higher, you don't need to use getContentPane(). Of course, you can use it just to be safe.
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18370

I'd like to expand on that subject. Since Java 5.0 it's not necessary to use getContentPane() for setting the layout and adding / removing components. These calls are automatically forwarded to the content pane. All other calls, like changing the background, should still be done using the content pane itself.

Before Java 5.0 you had to use getContentPane() for everything; setting the layout or adding / removing components directly would cause an error.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 136
Thanks a lot for quick response.
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18370

You're welcome.
 
 
subject: Difference when using getContentPane and when not
 
Threads others viewed
Problem with display of JComboBox
Please help me work on my "Swing"
I did it!!!! JApplet within JInternalFrame
Trouble with repainting Jframe (i think)
Setting the JFrame contentpane's border
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com