Programming Basic Java GUIs With Swing

When you first start learning programming, it is understood that when a program executes, it runs through a set of instructions, and terminates execution after the last instruction is run. Event driven programmer is an alternative to this idea, in which code is executed by the program in response to some sort of event being triggered. This type of programming is typically used when developing Graphic User Interfaces (GUI). GUIs appear in many ways, including webpages, phone apps, and desktop applications. The driving idea behind GUI programming is that a user is presented with some sort of interface they can interact with. When they do something on the interface, code is executed to react to the event.

When we design an interface, we create components, which are objects that a user can interact with. Components are then registered with listeners. Listeners wait for some event to happen and execute code when the event occurs. To understand this, let’s look at a simple Java GUI application written in Swing.

When we create a GUI in Java, we start by creating what is referred to as a JFrame. A JFrame can be thought of as the main window of our application, which will hold all of the other components the user can interact with. There are a number of different properties we can set on JFrames to make them behave as required. For example, the code below defines a JFrame with a title text “Title of the window”, sets the default close operation to exit the program, sets the size of the frame to 300 by 300, and makes the frame visible to the user.

Image for post
The initial setup of the JFrame

Once we have created and setup our JFrame, we can start adding components to it. If we wanted to add a button the frame, we can declare a new JButton, and attach it to the JFrame.

Image for post
Adding a button to the frame

When we create a new JButton, we provide a caption to display within the button, in this example, “Press me!”. We then add it to the content pane of the JFrame. A content pane is the area of the JFrame window that can hold components within it. When we run this program, we get a GUI like below.

Image for post
The resulting GUI

Currently, when the user interacts with the button, nothing will happen. To give this button functionality in the event that it is pressed, we need to define an ActionListener. An ActionListener is a special type of function that is linked to a button. When the button is interacted with, the ActionListener is called to execute any code we write into the function.

To create an ActionListener, typically we will create a new object which implements the ActionListener interface. Inside this class, we will create a function called actionPerformed, which will trigger when the user interacts with the button. The following example shows an ActionListener which will display an alert saying “Hello World!” when the button is clicked.

Image for post
Definition of a basic ActionListener

To register this ActionListener to our button, we use the addActionListener method.

Image for post
Adding the ActionListener to our button

Now, when we press the button, we will get an alert that displays “Hello World!”.

Image for post
The result of the ActionListener

When we call the actionPerformed method, it gets in a variable e, which holds information about the action that was performed on the button. Using this variable, we can determine things like which button was pressed. For example, we can modify the actionPerformed method to display the caption of the button that was pressed.

Image for post
Adjusting the listener to output the button text

These examples show how Java GUIs can be developed to allow user interaction. With programs developed in this way, the user is more in control of the application execution, which provides a more user-friendly experience compared to command-line based applications.

Leave a Reply

Your email address will not be published. Required fields are marked *