When to use:
Container c = getContentPane();
And when to use:
frame.getContentPane();
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.
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.
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
}
}
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.
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With