when should I use JFrame.add(component) and JFrame.getContentPane().add(component) in java

ksm001 picture ksm001 · Sep 26, 2011 · Viewed 8.8k times · Source

Is there a difference between them and are there any conditions in which one should be used instead of the other?

Answer

dogbane picture dogbane · Sep 26, 2011

Both calls are the same. In Java 5, they changed jframe.add to forward calls to the content pane.

From the Java 5 release notes:

Lastly, after seven years, we've made jFrame.add equivalent to jFrame.getContentPane().add().

Also, see the javadocs.