Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java Swing JFrame

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:

About Java Swing JFrame Example
About Java Swing JFrame Example

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.

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.

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.

Swing Content Pane
Swing Content Pane

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:

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: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.