1. Swing vs AWT Components
AWT components are considered as Heavy Weight Components and they depend on the underlying OS to draw them. So, it takes the resource of the Operating System. Whereas Java Graphics API draws Swing components and it makes them lightweight components. The swing components are built on top of the AWT components by cutting downs the OS dependency of drawing them. For example, Frame is an AWT component/container and JFrame is the swing component/container. One more example is Label (AWT) and JLabel (Swing).
2. About this Swing JFrame Example
In this example, we will create JFrame with three JLabels in it. Have a look at the JFrame Example below:

In the above example, we can see how a JFrame Window looks. The three buttons in the top right clearly show how it was drawn using line and rectangles using in-built graphics API. The window displays the title in the frame. And in the Gray area, three JLabels are sitting. That is all and nothing special here.
3. Swing JFrame Size and Position
We can create Swing Frame Window by sub-classing it from the JFrame class. This will allow customization and re-use. In the below code, we create JFrameDemo
and set its title by passing a string title to the super class constructor. Then the
setBounds method decides the size and position of the JFrame Window. First two param tells where the top left corner of the window is. The third and fourth param specifies the JFrame’s width and height.
1 2 3 4 5 6 |
public class JFrameDemo extends JFrame { public JFrameDemo(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(20, 20, 400, 210); |
4. ContentPane of a Swing Container
In Java Swing, the top-level containers manage its components via invisible pane. It is called Content Pane. For example, let us take the JFrame. Here, the menus, frame titles do not take part on the content pane. But the components like labels, text boxes, buttons etc. get added to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Sample 02: Set the Container object Container ControlHost = getContentPane(); //Sample 03: Create three JLabels JLabel lbl1 = new JLabel("Label 1"); JLabel lbl2 = new JLabel("Label 2"); JLabel lbl3 = new JLabel("Label 3"); //Sample 04: Set Layout Manager ControlHost.setLayout(new FlowLayout()); //Sample 05: Add Components to Container ControlHost.add(lbl1); ControlHost.add(lbl2); ControlHost.add(lbl3); |
In the above, we get the ContentPane of the frame window, by calling
getContentPane method. Then, we add the three JLabel
components to it. When the JFrame is displayed, it shows all three JLabels added to this pane. Also note, the ContentPane, which is a container, is managed by the FlowLayout Manager. Have a look at the below picture to know where exactly the ContentPane is layered in the Top-Level container.

This content pane does not cover the display of frame title and menus (Not shown here). The Gray Area shown below is the ContentPane and in our example, the FlowLayout manager is taking care of the control layout in it.
6. Set Default Close Action
Top-Level windows like JDialog and JFrame provides an API function setDefaultCloseOperation to tell what happens when user clicks the ‘X’ button on the Frame Window. In the below code, we use the constant EXIT_ON_CLOSE to tell Swing that we want to exit the application when the user clicks the close button:
1 2 |
//Sample 06: Set how to behave for Close Events setDefaultCloseOperation(EXIT_ON_CLOSE); |
The constants which we can pass to the setDefaultCloseOperation
for the function are below:
- EXIT_ON_CLOSE: This we used in out example.
- DISPOSE_ON_CLOSE – This will dispose the window after invoking the WindowListener objects.
- HIDE_ON_CLOSE – Hides the window after invoking the objects.
- DO_NOTHING_ON_CLOSE – The program should take of exit action.
7. Watch JFrame on Youtube
Categories: Swing
Tags: getContentPane, JFrame, setBounds, setDefaultCloseOperation