Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does getContentPane() do exactly?

When to use:

Container c = getContentPane();

And when to use:

frame.getContentPane();
like image 270
Chinmay Kale Avatar asked Sep 15 '14 15:09

Chinmay Kale


People also ask

How do I make a content pane?

In the Comments pane, you can see a single list of all comments in your document, including resolved comments. To switch between the contextual view and the Comments pane, simply click the Comments button in the upper right corner of your Word window.

What is a content pane Java?

JFrames have a content pane, which holds the components. These components are sized and positioned by the layout manager when JFrame's pack() is called. Content pane border. There are several ways to handle the content pane, but most of them fail to provide one basic requirement -- ability to set a border.


4 Answers

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
    }
}
like image 164
Ted Hopp Avatar answered Oct 19 '22 03:10

Ted Hopp


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

like image 41
Jaimin Patel Avatar answered Oct 19 '22 03:10

Jaimin Patel


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.

like image 2
Caffeinated Avatar answered Oct 19 '22 03:10

Caffeinated


Likely you are extending JFrame which means that the class will inherit the methods from JFrame. As such, your code may look somewhat like the following:

public class MyClass extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new MyClass();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MyClass() {
        ...
        Container c = getContentPane();
    }
}

In the above example, there is no need to use frame.getContentPane() because you are inheriting the methods of JFrame. In other words, you only need to write getContentPane(). Alternatively, in most cases you should actually be instantiating a new JFrame as an instance variable unless you are actually adding new functionality to the JFrame class:

public class MyClass {
    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new MyClass();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MyClass() {
        ...
        Container c = frame.getContentPane();
    }
}
like image 1
David Yee Avatar answered Oct 19 '22 01:10

David Yee