Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GUI: about getContentPane( ) method and content

Tags:

java

swing

jframe

In this piece of code:

JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

I can see it makes a new label and adds it to the JFrame object frame. But I want to understand what does getContentPane() do, and why do I need it?

I read this API but I still didn't understand.

like image 681
Bersan Avatar asked May 24 '13 21:05

Bersan


1 Answers

Every Swing top level container (and JInternalFrame) has what's called a JRootPane. This is responsible for actually managing the overall layout of the window.

enter image description here

The root pane has a number of layers, one of which is the content pane. When you add something to a frame (since Java 5 I think), it is automatically added to the content pane for you, before this, you had to call getContentPane().add(...) yourself

Take a look at How to use RootPanes

like image 109
MadProgrammer Avatar answered Oct 20 '22 04:10

MadProgrammer