Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getContentPane()?

 
Omo Jesu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Saw a code in a recent textbook saying everything must be added to the contentPane before being added to the Panel. But in another recent book, I was made to know that it is not compulsory that you can easily just use the add to the frame and it gets loaded to the contentPane automatically, which I have tried and it worked. Is there any difference in both? Please explain.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API for JFrame or JDialog. It's well explained there.

To get a better understanding of the role of the contentPane etc go through the API for RootPaneContainer.
 
Jason Hardaway
Greenhorn
Posts: 26
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before JDK 5, you had to call the getContentPane() method in order to get a reference to the content pane and after that, you had to call the add() method on that reference to add the component. Now, beginning with JDK 5 and forward, it is not necessary to make an explicit call to getContentPane() because a call to add() is already routed to the content pane when add() is called on a top-level container such as JFrame. Hope this helps you.
 
Omo Jesu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Read the API for JFrame or JDialog. It's well explained there.

To get a better understanding of the role of the contentPane etc go through the API for RootPaneContainer.


Thanks very much... Checking the RootPaneContainer API out now!

 
Omo Jesu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Hardaway wrote:Before JDK 5, you had to call the getContentPane() method in order to get a reference to the content pane and after that, you had to call the add() method on that reference to add the component. Now, beginning with JDK 5 and forward, it is not necessary to make an explicit call to getContentPane() because a call to add() is already routed to the content pane when add() is called on a top-level container such as JFrame. Hope this helps you.


Thanks... but was confused when Head First Java said we must add to ContentPane and the book covers Java 5.0
 
Jason Hardaway
Greenhorn
Posts: 26
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omo Jesu wrote:
Jason Hardaway wrote:Before JDK 5, you had to call the getContentPane() method in order to get a reference to the content pane and after that, you had to call the add() method on that reference to add the component. Now, beginning with JDK 5 and forward, it is not necessary to make an explicit call to getContentPane() because a call to add() is already routed to the content pane when add() is called on a top-level container such as JFrame. Hope this helps you.


Thanks... but was confused when Head First Java said we must add to ContentPane and the book covers Java 5.0

You are welcome. I would assume the authors did that because some programmers may have been using an earlier version before JDK5 and they wanted to make sure the code would work for all readers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic