Swing in Java

What is Swing?

Swing provides graphical user interface (GUI) components to develop Java applications with a rich set of graphics such as windows, buttons, checkboxes, etc. What is a GUI? Before we define a GUI, let us first explain a user interface (UI).

A program does three things:

  • Accepts inputs from the user
  • Processes the information, and
  • Produces outputs

A user interface provides a means to exchange information between a user and a program, regarding inputs and outputs. In other words, a user interface defines the way the interaction between the user and a program takes place. Typing text using a keyboard, selecting a menu item using a mouse, or clicking a button can provide input to a program. A program’s output can be displayed on a computer monitor in the form of character-based text, a graph such as a bar chart, a picture, etc.

You have written many Java programs. You have seen programs where users had to provide inputs to the program in the form of text entered on the console, and the program would print the output on the console. A user interface where the user’s input and the program’s output are in text form is a character-based user interface. A GUI lets users interact with a program using graphical elements called controls or widgets, using a keyboard, a mouse, and other devices.

The Simplest Swing Program

Let’s start with the simplest Swing program. You will display a JFrame, which is a top-level container with no components in it. To create and display a JFrame, you need to do the following:

  • Create a JFrame object.
  • Make it visible.

To create a JFrame object, you can use one of the constructors of the JFrame class. One of the constructors takes a string, which will be displayed as the title for the JFrame. Classes representing Swing components are in the javax.swing package, so is the JFrame class. The following snippet of code creates a JFrame object with its title set to “Simplest Swing”:

Create a JFrame object

JFrame frame = new JFrame(“Simplest Swing”);

When you create a JFrame object, by default, it is not visible. You need to call it’s setVisible(boolean visible) method to make it visible. If you pass true to this method, the JFrame is made visible, and if you pass false, it is made invisible.

Make the JFrame visible on the screen


That is all you have to do to develop your first Swing application! In fact, you can wrap the two statements, to create and display a JFrame, into one statement, like so:

new JFrame(“Simplest Swing”).setVisible(true);

		package com.jdojo.swing;
		import javax.swing.JFrame;
		public class SimplestSwing {
			public static void main(String[] args) {
				//Create a frame
				JFrame frame = new JFrame("Simplest Swing");
				//Display the frame
Swing in Java
Components of JFrame

We will display a JFrame in the previous section. It looked empty; however, it was not empty. When we create a JFrame, the following things are automatically done for us:

  • A container, called a root pane is added as the sole child of the JFrame. The root pane is a container. It is an object of the JRootPane class. You can get the reference of the root pane using the getRootPane() method of the JFrame class.
  • Two containers called glass pane, and layered pane is added to the root pane. By default, the glass pane is hidden, and it is placed on top of the layered pane. As the name suggests, the glass pane is transparent, and even if you make it visible, you can see through it. The layered pane is named because it can hold other containers or components in its different layers. Optionally, a layered pane can have a menu bar. However, a menu bar is not added by default when you create a JFrame. You can reference the glass pane and the layered pane by using the getGlassPane() and getLayeredPane() methods of the JFrame class, respectively.
  • A container called a content pane is added to the layered pane. By default, the content pane is empty. This is the container in which you are supposed to add all your Swing components, such as buttons, text fields, labels, etc. Most of the time, you will be working with the content pane of the JFrame. You can get the content pane’s reference by using the getContentPane() method of the JFrame class.
Swing in Java
Adding Components to a JFrame

		package com.jdojo.swing;

		import javax.swing.JFrame;
		import javax.swing.JButton;
		import java.awt.Container;
			public class AddingComponentToJFrame {
				public static void main(String[] args) {
					JFrame frame = new JFrame("Adding Component to JFrame");
					Container contentPane = frame.getContentPane();
					//Add a close button
					JButton closeButton = new JButton("Close");
					//set the size of the frame 300 x 200
					frame.setBounds(50, 50, 300, 200);
Swing in Java

Copyright 1999- Ducat Creative, All rights reserved.