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.
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.
There are basically two ways to create Swing applications, more precisely how forms can be created.
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 MainJFrame .
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.
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.
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:
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.
Downloaded 4x (19.93 kB)
Application includes source codes in language Java