Udemy logo

javaswingtutorialJava is a powerful programming language that is right at home in standalone desktop applications and complex enterprise-level web applications. It is a popular programming language for computer science students because it incorporates just about every key concept of modern object-oriented programming yet remains relatively easy to learn.

After you have graduated from console-based (command line) programs, you will want to start creating your own Graphical User Interfaces (GUIs). Fortunately, Java makes this easy thanks to the Swing GUI toolkit. Although creating GUIs with Java is relatively easy, you should still have a basic understanding of Java before starting. Check out the Introduction to Java Training Course if you are unfamiliar with basic Java programming concepts.

The Swing toolkit offers developers a platform-independent, customizable, configurable, and lightweight solution that can easily be incorporated into practically any Java program. The toolkit offers basic components like buttons and labels as well as advanced features including trees and tables. The entire toolkit is written in Java and is part of the Java Foundation Classes (JFC). The JFC is a collection of packages designed to create fully featured desktop applications. Other components of the JFC include AWT, Accessibility, Java 2-D, and Drag and Drop.

For the purposes of this tutorial, it is assumed you are using the Eclipse IDE. Although an IDE is not required, it makes programming, compiling, and running your program much easier. If you are unfamiliar with using an IDE to create Java programs, check out Java Programming Using Eclipse.

Creating a New Swing Project

From the homepage of Eclipse, click File and select New Java Project from the drop-down menu. In the setup window that appears, enter the name of your project and then click finish. At the Navigator (upper left-hand corner), right-click the SRC folder and select New Class. Enter the class name (in this example it should be HelloWorld) and click finish.

With the environment set up, you are now ready to start adding code and create a basic Hello World program using the Java Swing GUI toolkit. You should also consider checking out Mastering Java Swing for a more detailed overview of creating new projects and using the Swing toolkit.

Hello World GUI

As with any Java program, the first thing you need to do is import the libraries needed for the program. In this case, you only need to import one library using the statement:

import javax.swing.*;

The next step is to define the main method and create the class object. If you are unfamiliar with the “main method” terminology, you can brush up on your basic Java skills in Java Programming for Beginners. The code to define the main method and create the class object should look like this:

public class HelloWorld extends JFrame {

        public

static void main(String args[]) {

                    new

HelloWorld();

        }

As you can see above, this code creates a public class called HelloWorld. It’s also important to note that this public class extends JFrame; the basis of any Java GUI program. The main method follows and the third line creates a new instance of the HelloWorld class.

The JFrame is responsible for creating the window where your application “lives.” In its most basic form, a JFrame is a window with a title, border, and an optional menu bar. The frame is also where any components that your application uses are located such as the JLabel you’ll see below:

HelloWorld() {

                    JLabel jlbHelloWorld = new JLabel(“Hello World”);

                    add(jlbHelloWorld);

This code creates a new JLabel within the JFrame that displays the text “Hello World.” The third line adds the JLabel to the visible frame. JLabels can be used to display messages or images as needed. It is a quick and easy way to add text to your program which is why it works so well in this Hello World example.

The good news is that there are only a couple more lines of code and your first Java GUI program using the Swing toolkit will be complete. Here is what the completed program looks like:

import javax.swing.JFrame;

import javax.swing.JLabel;

//import statements

public class HelloWorld extends JFrame {

public static void main(String args[]) {

                    new HelloWorld();

        }

        HelloWorld()

{

                    JLabel jlbHelloWorld = new JLabel(“Hello World”);

                    add(jlbHelloWorld);

                    this.setSize(100,100);

                    setVisible(true);

        }

}

Notice that the size of this program is set to 100 pixels by 100 pixels. The JFrame class allows you to set the size of your application by default or you can choose to allow the user to change the size as needed. The next line – setVisible(true); – actually makes your application window show up on the user’s computer. As simple as this line seems, you would be surprised by how many new programmers forget to set the visibility of the JFrame. Without setting this value to true, the program will not be visible at runtime.

At this point, your Hello World program is complete and you can run it via the Eclipse IDE. Of course, there is a lot more you can do even with a basic example like this and the best way to learn is to experiment on your own. You could add buttons, text fields, or even images (using JLabel) to this simple program to make it more interesting and teach valuable programming skills along the way.

If you have become bored with basic command-line Java applications, learning how to use the Swing GUI toolkit is guaranteed to make your programs more user-friendly, interesting, and above all else, fun.

Page Last Updated: October 2013

Top courses in Java

Java for Beginners
Navin Reddy
4.5 (1,824)
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.5 (4,087)
Bestseller
Learn Selenium with Java, Cucumber & Frameworks
Pavan Kumar
4.6 (8,366)
Bestseller
Modern Java - Learn Java 8 Features By coding it
Pragmatic Code School
4.5 (11,325)
Java Interview Help
Bharath Thippireddy
4.5 (1,631)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo