Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference when using getContentPane and when not  RSS feed

 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for quick response.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!