This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JFrame.getContentPane() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame.getContentPane()" Watch "JFrame.getContentPane()" New topic


Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Hi all,
I want to add a component into top-level container.
In here, I use JFrame as top-level container.
My understanding is there are 2 ways to do that..
1. I use JFrame method getContentPane().add(component);
2. Because of JFrame is subclass of java.awt.Containter, I can use JFrame method add(component) directly.

Is it true?
Are there performance effect between both?

Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
i think they are same, JFrame method add(component) will inside call getContentPane().add(component) finally. Not 100% sure.

Download Java GUI Builder, <a href="" target="_blank" rel="nofollow"></a>
Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Calling add() on the frame directly will give a runtime error:

java.lang.Error: Do not use add() use getContentPane().add() instead
[ November 15, 2004: Message edited by: Sebastiaan Kortleven ]
Ted Reynolds

Joined: Mar 16, 2004
Posts: 20
I thought that in 1.5 they changed it so that the two methods work the same. But of course if you are working with anything from 1.2 to 1.4 you need to use the getContentPane().add().
I agree. Here's the link:
subject: JFrame.getContentPane()
It's not a secret anymore!