Java Swing Tutorial for Beginners

1. Introduction

In Java, it provides a rich set of libraries to create Graphical User Interface (GUI), to make it work independently on different platforms. Swing is definitely the one which is most popular used, because its properties of light-weight, rich-control, easy-customizable.

Before Java Swing, Abstract Windowing Toolkit (AWT) is the one to create different GUIs. However, because of its disadvantages on platform-dependent, heavy-weight, few-components, it has been replaced by Java Swing, which is built on top to the AWT package. Note that some AWT components remain in Java and in some situations it must be used. In the following part, you’ll see some AWT package to be imported by our examples.

In this article, we focus on Java Swing, which has large set of components, such as JFrame, JButton, JMenu, etc. we’ll cover the most-often-used components in Java Swing and each component will be explained with example. Finally we’ll combine all these components to create a small GUI project.

For the following examples, Java 8 and Eclipse IDE (version Mars 4.5.0) are used.

2. Java Swing Components

2.1 JFrame

In Java Swing, most application will be built inside a basic component JFrame, which creates a window to hold other components.

Note that we can initialize the frame with string “Hello Swing”, which creates a blank frame with title “Hello Swing” like the figure below. The function setDefaultCloseOperation tells the frame what to do when user close the frame and JFrame.EXIT_ON_CLOSE reforms exit when user close the frame. The size of the frame could be set by setSize with width and height parameters. Notice that without the setVisible function with parameter true, you won’t see anything on the screen. In order to see the GUI part, we need this function and set it to be true.

JFrame

2.2 JLabel

JLabel is a area to display a short string or an image or both. Normally we can add the JLabel into the JFrame we’ve built in previous part and show different displays. With the following java code added following the creation of JFrame, a label with text “I’m a JLabel” is created.

For the position of the label, it could be specified by JLabel.LEFT, JLabel.CENTER, JLabel.RIGHT, of which the position could set to be left, center and right correspondently. The figure below shows that a label is set in the center of the window.

JLabel

2.3 JPanel

JPanel is a popular container to hold different components. It could be set and added by using the code similar to following:

JPanel panel = new JPanel();
frame.add(panel);

2.4 JButton

JButton is an implementation of a “push” button. It can be pressed and configured to have different actions, using the event listener. For this part we’ll discuss it in the last. In the following code, we added three buttons into the panel with different name on them: Button 1, Button 2, Button 3.

In the example above, we set a label to be in the up side and a panel containing three buttons in the bottom. The result is shown in the figure below. Note that we have a BoxLayout function, which is used to set the layout of the components. Further inflammation can be found in my another article about BoxLayout, which used the JButton a lot.

JButton

2.5 JRadioButton

Here, JRadioButton is quite different from JButton. It’s a radio button that can be selected or deselected. Use with the ButtonGroup object to create a group of buttons,in which only one button can be selected at a time.

The code above creates a button group and have there radio button inside. We set the “Male” button to be chosen by default, by setting it to be true initially. And we can see figures below, showing that each time, only one button can be chosen.

JRadioButton1

JRadioButton2

2.6 JCheckBox

JCheckBox is used to create checkbox, of which multiple checkbox could be selected at the same time. That’s the main difference between JRadioButton. For JCheckBox, I’ve written another article on it with detailed example, you can check it here.

2.7 JSlider

JSlider is a component that lets the users select a value by sliding a knob within a specified interval. For the knob, it always points to the point which matches the integer values within the interval. You can check my article on JSliderhere.

2.8 JTable

JTable is used to create a regular two-dimensional tables. The table can display data inside of it. In addition, the user can also edit the data. The following example shows that we created three columns: ID, Name, Age. In each column, we have set the data/information inside.

2.9 JComboBox

JComboBox is a component to select value from a drop-down list. You can choose one and only one element from the list. The following example shows how to create the drop-down list, from which different countries can be chosen: “Australia”, “China”, “England”, “Russia”, “United States”.

2.10 JMenu

In this example, we have different settings on menu. We have File menu, which includes of save, quit submenu, and Edit menu, including copy, cut, paste submenu, and Help menu, containing try your best:)

3. A small Java Swing project

In this example, we’ll combine what we’ve learnt from the previous part and try to use them in a small project: a simple calculator with GUI display. In this project, we’ll add event listener into the system, and when we press the button, there’ll be some reactions to it.

The following code is for the simple calculator project. Note that it’s just a basic one, which supports only the basic add, subtract, multiply, divide operations. Most of the code could be referred here.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.