From the Trenches of the Enterprise Software

Yakov Fain

Subscribe to Yakov Fain: eMailAlertsEmail Alerts
Get Yakov Fain via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: Java Developer Magazine

Java Developer : Article

Lesson 12: An Introduction to Graphical User Interfaces with Swing - Part I

Everything is a little better, faster, and more convenient in Swing

Java comes with the whole bunch of classes that you'll be using to create graphical applications. There are two main groups of classes (libraries) that are used for creating windows in Java: AWT and Swing.

AWT and Swing
When Java was originally created, only AWT library was available for working with graphics. This library is a simple set of classes like Button, TextField, Label and others. Pretty soon, another and more advanced library called Swing was introduced. It also includes buttons, text fields, and other window controls. The names of the Swing components start with the letter J, for example JButton, JTextField, JLabel, and so on.

Everything is a little better, faster, and more convenient in Swing, but you can use it only if you're sure that your programs will run on the computers with JVMs that support Swing classes. In this lesson we'll create a simple calculator program using Swing.

Note. There is yet another set of Java classes which is a part of Eclipse platform called Standard Widget Toolkit (SWT), but since it's not a part of Java SDK, we won't discuss it here.

Packages and Import Statements
Java comes with many classes that are organized in packages. Some packages include classes responsible for drawing, while other packages have classes to work with the Internet, and so on. For example the class String is located in the package called java.lang, and the full name of the class String is java.lang.String.

Java compiler knows where to find classes that are located in java.lang, but there are many other packages with useful classes, and it's your responsibility to let the compiler know where the classes from your program live. For example, most of the Swing classes live in one of the following two packages:


It would be annoying to write a full class name every time you use it, and to avoid this you can write import statements just once above the class declaration line, for example:

import javax.swing.JFrame;
import javax.swing.JButton;

class Calculator{
   JButton myButton = new JButton();
   JFrame myFrame = new JFrame();

These import statements will allow you to use the short class names like JFrame or JButton, and Java compiler will know where to look for these classes.

If your need to use several classes from the same package, you do not have to list each of them in the import statement, just use the wild card. In the following example the star (asterisk) makes all classes from the package javax.swing visible to your program:

import javax.swing.*;

Still, it's better to use separate import statements, so you can see what exactly the class is importing from each package. You can find more about packages in the lesson on packages and imports (http://java.sys-con.com/read/49108.htm).

Major Swing Elements
These are some of the major objects that Swing applications consist of:

  • A window or a frame that can be created using the class JFrame.
  • An invisible panel or a pane that holds all these buttons, text fields, labels, and other components. Panels are created by the class JPanel.
  • Window controls like buttons (JButton), text fields (JTextfield), lists (JList), and so on.
  • Layout managers that help arrange all these buttons and fields on a panel.
Usually a program creates an instance of a JPanel and assigns the layout manager to it. Then, it can create some window controls and add them to the panel. After that, add the panel to the frame, set the frame's size and make it visible.

But displaying a frame is only half of the job, because the window controls should know how to respond to various events, for example a click on the button.

In this Lesson we'll learn how to display nice-looking windows, and the next lesson  is about writing code that will respond to events that may happen with elements of this window.

Our next goal is to create a simple calculator that knows how to add two numbers and display the result. Create a new project in Eclipse named My Calculator and add a new class SimpleCalculator with the following code:

import javax.swing.*;
import java.awt.FlowLayout;

public class SimpleCalculator {
public static void main(String[] args) {
// Create a panel
JPanel windowContent= new JPanel();

// Set a layout manager for this panel
FlowLayout fl = new FlowLayout();
// Create controls in memory
JLabel label1 = new JLabel("Number 1:");
JTextField field1 = new JTextField(10);
JLabel label2 = new JLabel("Number 2:");
JTextField field2 = new JTextField(10);
JLabel label3 = new JLabel("Sum:");
JTextField result = new JTextField(10);
JButton go = new JButton("Add");

// Add controls to the panel

// Create the frame and add the panel to it
JFrame frame = new JFrame("My First Calculator");


// set the size and make the window visible
Compile and run this program and it'll display a window that looks like this one:

More Stories By Yakov Fain

Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.