Swing Tutorial 1: Creating a Basic Swing Application

Guest Posts ljc1999

In this first swing tutorial we're going to create a very basic "Hello World" swing application.
It is better to have some previous knowledge of Java before using Swing, but not essential.



Assuming you have JDK installed and eclipse installed and open, click File -> New -> Java Project.
Name it whatever you like, then press Finish.
Next you need to create a class,  if you don't know how Right-click your project -> New -> Class, name it whatever you like.

Inside your new class, that contains this:

public class Main {

}


Add a main method:

public class Main {
   public static void  main(String[] args) {

   }
}


This is now a working Java application, but it doesn't do anything yet!
Lets make it open a GUI window using Swings JFrame class.

Inside the main method you need to declare a new instance of JFrame:

JFrame frame = new JFrame("Hello World");


I call my instance frame, and the String inside the constructor is the title of the window that we are going to make.

As you may see the JFrame has an error and is underlined in red, that's because its not imported so we need to import it. You can press CTRL + Shift + O (CMD + Shift + O on mac) to automatically import, or you can do it manually by adding this to the top of your class:

import javax.swing.JFrame;


Now the red underline should go away.

If you run this now it will create a JFrame but you cant see it, thats because by default its not visible.
To make it visible you need to add this under the JFrame line.

frame.setVisible(true);


That's all there is to a extremely basic JFrame but if you run it there are a couple of problems:
If you click the close button on your window, it just makes the JFrame invisible again but doesn't exit your application.
Let's fix it:

Add this, it tells your application to exit when you press the close button:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


The second problem is that the window is tiny, lets fix it:

Add this, it sets the size to 600 pixels by 500 pixels. You can change the size though:

frame.setSize(600, 500);


Your basically done, but if you want program to run more smoothly and be done in the approved way, you should run your JFrame in a new thread. If your not sure what to do its best to do this, better safe that sorry!

Put this in your main method then move your JFrame code inside this, SwingUtilities also needs to be imported:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {

    }
});


Your complete class should now look like this:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main {
   public static void  main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
               JFrame frame = new JFrame("Hello World");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
               frame.setSize(600, 500);
           }
       });
   }
}