Way2Java

Java AWT Panels

Java AWT Panels play a very important role in layout management. Panels in combination with layout managers render the desired layout to the programmer.

Following hierarchy gives the place of Panel in AWT API.

java.lang.Object –> java.awt.Component –> java.awt.Container –> java.awt.Panel –> java.applet.Applet

Properties of Java AWT Panels
  1. As you can see from the hierarchy, a panel is both a component and a container as it is a subclass of both Component and Container. As a component it can be added to another container and as a container it can be added with components. Panels work both ways.
  2. The default layout manager for panel is FlowLayout which can be changed as per the requirement of the layout.
  3. It is known as a child window. As a child window, it does not have a border.
  4. Panels can be nested (known as nested panels) for better layout presentation.

Following program illustrates the panels' usage in layout management. In the following program, three buttons are added to the north (top) of the frame and three buttons to the south (bottom) of the frame. Without panels, this arrangement is not possible with mere layout managers.

Example on Java AWT Panels
import java.awt.*;
public class PanelsDemo extends Frame 
{
    public PanelsDemo()
    {
       setLayout(new BorderLayout());     
                                                 // creating panels
       Panel p1 = new Panel();
       Panel p2 = new Panel();
                                         // setting some properties
       p1.setBackground(Color.yellow);
       p2.setBackground(Color.magenta);
       p2.setLayout(new GridLayout(1, 3, 20, 0));
                                          // create some components, say 6
       Button b1 = new Button("OK 1");
       Button b2 = new Button("OK 2");
       Button b3 = new Button("OK 3");
       Button b4 = new Button("OK 4");
       Button b5 = new Button("OK 5");
       Button b6 = new Button("OK 6");
                                       // add 3 buttons to Panel p1
       p1.add(b1);           
       p1.add(b2);      
       p1.add(b3);
                                        // add 3 buttons to Panel p2
       p2.add(b4);           
       p2.add(b5);      
       p2.add(b6);
                                         // add panels to frame
       add(p1, "North");
       add(p2, "South");

       setTitle("Learning Panels");
       setSize(300, 300);	
       setVisible(true);
     }
     public static void main(String args[])
     {
       new PanelsDemo();
     }
}



Output screen of Java AWT Panels

Panel p1 = new Panel();
Panel p2 = new Panel();

Two panels, p1 and p2 are created and set background colors to know the space occupied by them.

p2.setLayout(new GridLayout(1, 3, 20, 0));

The default layout manager for a panel is FlowLayout. The panel p1 default layout manager is not disturbed. But for practice sake, the layout of p2 is changed to GridLayout with 1 row, 3 columns, 20 horizontal gap and 0 vertical gap (as only one row exist).

p1.add(b1); p1.add(b2); p1.add(b3);

Three buttons b1, b2 and b3 are added to the panel p1. Similarly, buttons b4, b5 and b6 are added to panel p2.

add(p1, “North”);
add(p2, “South”);

Panel p1 is added to the north of the frame and p2 is added to the south. Along with panel p1, three buttons go and stay on the frame on north-side. But on the account of frame only one component p1 exists. Like this, panels can be added to the east and west of the frame also.

Note: For simplicity, the window closing code is not included. For closing this frame, 4 styles exist and you can apply any one to this program to close the frame.

AWT Window

A window is that one which can hold components; for example, frame is a window. In AWT, windows are of two types – Top-level and child.

1. Top-level Window (Frame and Applet)

A top-level window exists by itself, or to say, it has an independent existence. A top-level window comes with a border and title bar etc. as per the operating system's window decorations. The top-level window can be moved anywhere on the monitor and also can be resized. One Java application can have any number of windows (say, frames). One frame, being top-level, cannot be added to another frame. A frame can exist beside another; a frame cannot exist inside another frame (it is quiet contrary to child windows).

2. Child Window (Panel)

In Java, a child window does not have a border. Panel, being a child window, does not have a border. That is, you cannot see a panel. But still you can visualize the space taken by a panel by giving a background color to the panel. A child window cannot exist by itself. It comes into existence only when added to a top-level window. Any number of child windows can be added to a top-level window; that is, a frame can be added with a number of panels. A child window can be added to another. That is, panels can be nested for better layout arrangement and known as nested panels.