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 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.
- 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.
- 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:
Right-click the created project in the Projects window and add a new JFrame form:
It'll be the main window of our application. As the name, we'll choose
Your NetBeans window should now look like this:
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
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:
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.
No one has commented yet - be the first!