Sunday, 27 January 2013

Getting Access to the Content Pane


The class JFrame has the method getContentPane that can be used to access the content pane of the window. However, the class JFrame does not have the necessary tools to manage the components of the content pane. The components of the content pane are managed by declaring a reference variable of the Container type and then using the method getContentPane.  This can be done as follows:

Container pane = getContentPane();

The class Container is in the package java.awt.  Some methods of the class are:

public void add(Object obj)
public void setLayout(Object obj)

The method setLayout is used to set the layout of the content pane. To set the layout of the container to a grid, you use the class GridLayout. Consider the following statement:

pane.setLayout(new GridLayout(5, 2));

This statement creates an object belonging to the class GridLayout and assigns that object as the layout of the content pane, pane, by invoking the setLayout method.

If you do not specify a layout, Java uses a default layout. If you specify a layout, you must set the layout before adding any components.

Teaching Tip

Java provides many layout managers. A tutorial on layout managers for Java can be found at:

No comments:

Post a Comment