What does getContentPane() do exactly?

getContentPane().setBackground(Color.YELLOW);

This line of code is difficult to understand, and the tutor will lay the foundation for you to understand it fully as you continue to study Java. First to consider is the rule about modifying an object with a method. On the left side of a period is an object, and the method that modifies the object is on the right side of the period. That rule applies here.

A container has several layers in it. You can think of a layer as a transparent film that overlays the container. 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. The content pane is an object created by the Java run time environment. You do not have to know the name of the content pane to use it. When you use getContentPane(), the content pane object then is substituted there so that you can apply a method to it. In this line of code, we are not adding an object to the content pane. Rather, we are making the color of the content pane to be yellow. This line of code is what changes the default color, white, to yellow, and you may recall seeing the yellow rectangle in the example the program running in a browser. This line of code is what made that rectangular area yellow.

One way to think about this is to think that the content pane object is substituted for the getContentPane() method, like this:

contentpaneobject.setBackground(Color.YELLOW);

Although you never really see the above statement, you do have the functionality of the statement. When you retrieve the content pane with the getContentPane() method, you can then modify the content pane object, which is arbitrarily named contentpaneobject in the example above. In this statement, the modification is to change the color of the content pane. That step is presented next in the tutor.

Notice the form of getContentPane() as a method. The method begins with a lower case letter, and it has parentheses. The parentheses are empty.

enter image description here

enter image description here


If the code is part of a JFrame subclass, you should use getContentPane(). If the code is not part of the frame (perhaps you're in the static main() method for the application), then you need to use a JFrame object to call getContentPane(); that's what frame.getContentPane() does.

Examples:

public class TestApp extends JFrame {
    public static void main(String[] args) {
        TestApp frame = new TestApp();
        Container c = frame.getContentPane();
        // do something with c
        frame.pack();
        frame.show();
    }

    /* constructor */
    public TestApp() {
        Container c = getContentPane(); // same as this.getContentPane()
        // initialize contents of frame
    }
}

Well, I could direct to the api :

Returns the contentPane object for this frame.

It's all part of the gui initialization process. Java's protocol really, admittedly some boilerplate to get your GUI up:

public class FlowLayoutExample extends JApplet {

  public void init () {
    getContentPane().setLayout(new FlowLayout ());
    getContentPane().add(new JButton("One"));
    getContentPane().add(new JButton("Two"));
    getContentPane().add(new JButton("Three"));
    getContentPane().add(new JButton("Four"));
    getContentPane().add(new JButton("Five"));
    getContentPane().add(new JButton("Six"));
  }
}

-Source

But essentially, we're obtaining the content pane layer so that you can later add an object to it. See this for more details.