Reputation: 1
I've visit a lot of tutorials about Java Swing, and i was wondering was the difference between adding a component using <ObjectJFame>.getContentPane().add();
or using <ObjectJFrame>.add()
,
and which is the best? (if there is a "better").
Upvotes: 0
Views: 771
Reputation: 1911
Read the documentation.
... 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);
...
So there should be no difference.
Upvotes: 0
Reputation: 10696
There is effectively no difference between getContentPane().add()
and myJFrame.add()
. I think it was back in the big Java 2 (JDK 1.5) revamp that adding directly to a top level UI component just forwarded to the underlying content pane.
Personally, I use getContentPane()
for the sake of clarity, but whatever you do, be consistent about it so that you (including the future you) and other people) will know what's happening.
Upvotes: 2
Reputation: 189
As long as I used the Swing library, I always add component using the second method. Actually, both functions do exactly the same thing.
Upvotes: 0