Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 204
ksm001 Avatar asked Sep 26 '11 11:09

ksm001


People also ask

What is the purpose of getContentPane () method in swings?

In Java Swing, the layer that is used to hold objects is called the content pane. Objects are added to the content pane layer of the container. The getContentPane() method retrieves the content pane layer so that you can add an object to it.

Which of the following is correctly to create instance of JFrame?

To create a JFrame, we have to create the instance of JFrame class. We have many constructors to create a JFrame. JFrame(GraphicsConfiguration gc): It creates a frame having a blank title and graphics configuration of the screen of device. JFrame(String title): It creates a JFrame having a title.

Is JFrame a component?

JFrame class is a type of container which inherits the java. awt. Frame class. JFrame works like the main window where components like labels, buttons, textfields are added to create a GUI.


1 Answers

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.

like image 199
dogbane Avatar answered Sep 19 '22 15:09

dogbane