Get up to 80 % extra points for free! More info:

Lesson 1 - Introduction to form applications in Java Swing

Welcome to the first lesson of an online course where we'll learn how to create window (form) applications in Java and try to use individual components. We'll work with buttons, text fields, tables, but also with pictures, timers, and other components available in these applications.

I'll write the series in such a way that you don't need any advanced knowledge. However, I suppose you know at least the basics of object-oriented programming.

Swing

Swing is a framework that allows to create form applications simply. We'll find there a full set of prepared components for most situations. And, if we need more, of course, we can create our own or modify an existing one. This series will be all about Swing.

Swing is based on AWT, an older framework that encapsulates the interface of the operating system itself. Compared to AWT, Swing provides richer and more comfortable components, plus supports the MVC architecture, we'll see all this further in the series.

At present, besides Swing, there's also a more modern framework, and that's JavaFX. It includes better separation of the logic and output, supports faster rendering, animations, multimedia, and other new technologies. However, it has more complex installation. In practice, both frameworks are used for developing form applications.

First form application

Let's start with nothing else but the classic Hello World application. This time, it'll be a form application :) If you haven't read the previous courses here, let me repeat that it'll be a simple application that does nothing but prints some text.

Ways to design a form

There are basically two ways to create Swing applications, more precisely how forms can be created.

  1. Graphic Designer - Modern IDEs allow us to create forms using a graphic designer. Working with a designer is very simple. We drag-and-drop components to the form and the IDE generates its source code. This method is ideal for most applications and saves a lot of work.
  2. Manual design - The code of the form is written by the programmer himself. Creating forms like this, mainly the larger ones, is very challenging and you need to position the elements manually using different spaces, panels, tables, and so on. This part is done by IDE for us in the first method. The manual form design is especially popular in schools and we'll skip it in this course.

Create a new project in NetBeans, select a standard Java Application as the project type. Choose HelloSwing as the name and uncheck Create Main Class:

Creating a new Java Swing project in the NetBeans IDE - Form Applications in Java Swing

Right-click the created project in the Projects window and add a new JFrame form:

New JFrame Form in NetBeans - Form Applications in Java Swing

It'll be the main window of our application. As the name, we'll choose MainJFrame.

Your NetBeans window should now look like this:

NetBeans Java applications using Java Swing - Form Applications in Java Swing

Let's describe its individual parts that we'll use to develop form applications. Important parts are highlighted in red in the picture above.

  • Designer - In Designer, we can see how the form looks like. So far, it's just an empty window (empty area).
  • Properties - In the Properties window, we can see the properties of the currently selected component on the form. If you don't see the window, turn it on in the menu View -> IDE Tools.
  • Palette - In the palette, you can find the components you can insert onto the form.
  • The Source / Design buttons toggle between the graphic designer and the form's source code. We're about to describe the code in a moment.

Setting component properties

When we select a component on the form or the form itself, we can change the properties of the component in the Properties window.

Since we have no element on the form yet, the form itself is selected. We'll set the title of the form to Greetings. Find the title property and enter the value Greetings. In this way, we'll set the properties of all the components on the form.

Adding a component to a form

Now, select Label in the palette. The label is a text component. In the palette, always choose the Swing components. You can find the original AWT components below in the palette, but we'll not use them. Drag-and-drop the label onto the form. Scale down the form and simply drag-and-drop the label to the center with your mouse. Set the label text to Greetings from the form in the Properties window.

You can run your first window application with the green Play button or the F6 key as always. When you first run the app, you'll be asked to select the main application class, select the class of the form and confirm. You should get something like this:

The first Java Swing window application in NetBeans - Form Applications in Java Swing

You can download the application below in case something didn't work for you. You can do so in every lesson of this course. Next time, in the lesson Java Swing under the hood, we'll explain how the application works under the hood, that is, the generated source code.


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 4x (19.93 kB)
Application includes source codes in language Java

 

All articles in this section
Form Applications in Java Swing
Skip article
(not recommended)
Java Swing under the hood
Article has been written for you by David Capka Hartinger
Avatar
User rating:
No one has rated this quite yet, be the first one!
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities