Java Swing Hello World Example Tutorial

In this article, we will learn how to create a simple Swing hello world program. We also learn how to compile and run a Swing application from the command line.
Let's get started!.
Here are the steps you need to follow:
  1. Install the latest release of the Java SE platform
  2. Create a program that uses Swing components
  3. Compile the program
  4. Run the program
  5. Output

1. Install the Latest Release of the Java SE Platform

Install the latest release of the Java SE platform, if you haven't already done so. You can download the latest release of the JDK for free from http://www.oracle.com/technetwork/java/javase/downloads/index.html.

2. Create a Program That Uses Swing Components

Let's create a simple Swing program called HelloWorldSwing, that brings up the GUI shown in Output section.
First, create a directory and name it as "swing-examples". Now create a Java class named HelloWorldSwing.java and add the following code to it:
package net.javaguides.javaswing.examples;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;

/**
 * 
 * This class demonstrates the simple hello world swing program.
 * @Ramesh Fadatare
 */
public class HelloWorldSwing {

    private static void createAndShowGUI() {
        JFrame jFrame = new JFrame("Hello World Swing Example");
        jFrame.setLayout(new FlowLayout());
        jFrame.setSize(500, 360);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JLabel label = new JLabel("Hello World Swing");
        Border border = BorderFactory.createLineBorder(Color.BLACK);
        label.setBorder(border);
        label.setPreferredSize(new Dimension(150, 100));

        label.setText("Hello World Swing");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);

        jFrame.add(label);
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        createAndShowGUI();
    }
}
Let's understand the above Swing hello world program.

Creating a JFrame window

From the above program, we created a JFrame just like any other Java objects:
JFrame jFrame = new JFrame("Hello World Swing Example");

Setting the layout manager

The default layout of the frame is BorderLayout, here we are setting FlowLayout layout like this:
jFrame.setLayout(new FlowLayout());

Adding child components

We are adding a label to JFrame like this:
JLabel label = new JLabel("Hello World Swing");
Border border = BorderFactory.createLineBorder(Color.BLACK);
label.setBorder(border);
label.setPreferredSize(new Dimension(150, 100));

label.setText("Hello World Swing");
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);

jFrame.add(label);

Specifying window closing behavior

We can specify which action will be executed when the user clicks on the frame’s close button. Here we are setting up exit the program (JVM terminates) after the window closes:
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Showing the frame on the screen

Make sure we set size for the frame before making it visible:
jFrame.setSize(500, 360);
jFrame.setVisible(true);

3. Compile the Program

Your next step is to compile the program. To compile the example, from the directory "swing-examples" the HelloWorldSwing.java file:
javac swing-examples/HelloWorldSwing.java
If you prefer, you may compile the example from within the "swing-examples" directory:
javac HelloWorldSwing.java

4. Run the Program

After you compile the program successfully, you can run it. From the directory above the "swing-examples" directory:
java swing-examplesHelloWorldSwing
If you prefer, you may run the example from within the "swing-examples" directory:
javac HelloWorldSwing.java

5. Output

Here is the output of this Swing hello world program:

Related Swing Examples

  • Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
  • Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
  • Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.

Comments