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