Results 1 to 4 of 4
- 02-17-2011, 06:27 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 10
Difference between add() and getContentPane().add()?
What is the difference between getContentPane().add() and just add();
Java Code:JFrame frame = new JFrame(); JButton button = new JButton("Corn"); [COLOR="Red"]frame.add(button); [/COLOR] // or............... [COLOR="Red"] frame.getContentPane().add(button);[/COLOR]
- What is the difference between getContentPane().add() and just add();
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:
frame.add(child);
And the child will be added to the contentPane. The content pane will always be non-null.
- 02-17-2011, 07:06 PM #3
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 10
Thanks gain :)
The API is the first place i looked and I know I read that very quote in the JFrame API. My issue is...
Nothing. add() was added to JFrame as a convenience method
makes more sense then
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:
frame.add(child);
And the child will be added to the contentPane. The content pane will always be non-null.
I guess i missed it :/ but now I see.
- 02-17-2011, 07:18 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Well, it can be a bit tricky: the JFrame class doesn't implement an add( ... ) method at all. It leaves that to a superclass, the Container class. That method implements an add( ... ) method and calls a protected addImpl( ... ) method. That's the method that is overridden by the JFrame class and it 'delegates' (an OO-word) the same to its content pane. It doesn't clearly show from the API documentation on a first read.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
what is the difference
By ron87 in forum New To JavaReplies: 5Last Post: 01-04-2011, 05:31 PM -
getContentPane update from actionPerformed method
By cazmo in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:35 AM -
What is the difference between
By arnab321 in forum New To JavaReplies: 2Last Post: 01-19-2009, 05:49 AM -
difference
By nishant in forum New To JavaReplies: 2Last Post: 09-17-2008, 07:04 PM
Bookmarks