How to Write Your First Java GUI Application

Java uses a set of features provided by JFC (Java Foundation Classes) to create graphical user interfaces (GUIs). JFCs are part of the Java platform and are therefore cross-platform.

These classes provide features such as pluggable look-and-feel, internationalization, and Swing GUI components.

This tutorial concentrates on how to use Swing components to create your first Java GUI application.

Swing Components

Swing is a JFC package that contains lightweight GUI components. It has 18 public packages. Fortunately, most of your applications will only need a few of them. In particular, javax.swing and javax.swing.event (less often).

For your application to appear on-screen, all the GUI components need to be part of a containment hierarchy. A containment hierarchy is a tree of components whose root is a top-level container.

Swing has three top-level container classes:

  • JFrame, used to create the main window/frame
  • JDialog, used to create dialog boxes
  • JApplet, used to add Swing components to applets

The focus of this tutorial will be how to use the JFrame top-level container. Each top-level container generally consists of a content pane and optionally, a menu bar.

Related: Types of Programming Errors and How to Avoid Them

The content pane is a space into which you can place visible components of a top-level container. A menu bar contains a list of items on your GUI.

MAKEUSEOF VIDEO OF THE DAY

Creating a Frame

A frame is the main window of your application and generally has a title and borders. To create a frame, use the JFrame container class:

JFrame frame = new JFrame("My Title");

The class has a constructor that allows you to define the title of your window. However, instantiating a frame alone is not enough to make your window appear on-screen.

You need to set the setVisible() method to true for your application to appear. With this minimal code, your application can now appear:

import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("HelloWorld");
frame.setVisible(true);
}
}

If you ran the code above, you most probably took at least a minute darting around your screen while looking for the application window. You most probably found a barely visible window at the edge of your computer screen.

Apart from this, you’ll notice that the program doesn’t stop running when you click the cancel button on your application window. You can notice this from your command line.

Related: A Beginner’s Guide to Using Interfaces in Java

The window will close, but the program will continue running in the background. This is because the default behavior is to hide the frame on exit. More useful behavior might be to exit the program or shift control to another program.


To solve these two issues, you’ll need to use the setSize() and setDefaultCloseOperation() methods. See the code below:

import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("HelloWorld");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}

In the example above, the window-closing event defined is EXIT_ON_CLOSE. This means that your program will exit the application when you close the frame. There are other window-closing events that you can use:

  • HIDE_ON_CLOSE: This is the default operation which hides the window on closing, but leaves it displayable.
  • DO_NOTHING_ON_CLOSE: This will perform no action when the user requests to close the window. Instead, a registered WindowListener handles closing with its windowClosing() method.
  • DISPOSE_ON_CLOSE: This hides the windows and also frees the resources the program had assigned to it.

Your First Python GUI Application

Now that you’ve learned how to create your first Java GUI application, it’s time to explore how to do so in Python. Python offers you the simplicity of less code to create your application.

It’s worth your time to learn how to use Python’s Tkinter library to create your GUI applications.


Desktop user interface with Python logo

Start Creating Desktop Apps in Python With the Tkinter GUI Library

Read Next


Leave a Comment