Build first GUI Application in Java using NetBeans IDE
This tutorial is an introduction to GUI applications in Java using NetBeans IDE.
The picture above looks like a login window, right? Yes. If you see, we have two input fields (text fields) to type Email ID and Password. We also see a Login button to login into the form. This window is an example of a GUI application.
Getting Started with GUI Applications in NetBeans IDE in Java
Let’s understand what GUI applications are. GUI means Graphical User Interface. It is an interface that allows a user to communicate with a device using graphical components like text field, button, check box, text area, etc. The user provides input data with the help of these components, and the application gives some results. The result may be displaying some information based on the data or storing the data in a location.
NetBeans IDE makes it effortless for us to create such applications using the Java programming language. It provides a palette from which we can drag and drop the components onto a frame. Following that, we can write some code for the components. In NetBeans IDE, we refer to this frame as JFrame and the components as jbutton, jtextfield, jlabel etc.
To design an application, first create a project. Open NetBeans IDE and follow the steps below.
Creating a New Project in NetBeans IDE in Java
- Choose File > Select New Project. Alternatively, you can click the New Project icon in the toolbar.
- A window will show up. Select Java from Categories pane and Java Application from Projects pane. Click Next.
- Enter the desired name for your project in the Project Name field. By default, NetBeans saves your project in the NetBeansProjects folder in My Documents.
- You can uncheck the Create Main Class field.
- Click Finish.
NetBeans creates a folder for your project in the projects window on the left. This folder will contain all the associated files and forms of the project.
Now, let’s create a new form.
Creating a New Form in NetBeans IDE – Java
- Go to the projects window. Then right-click on the project that you created and choose New > JFrame Form.
- Enter the name of the form in the Class Name field and the project name in the Package field.
- Click Finish.
Understanding Different Windows
Design Area: This is the primary window for creating and editing Java GUI forms. It has a toolbar that contains Design, Source and History toggle buttons. The Design window has the JFrame where we design the forms, while in the Source window, we write the code for the components.
Palette Window: This window contains a customizable list of all the components required to create an application. It has few sub-categories like Swing Controls, Swing Containers, Swing Menus, etc. We will use Swing Controls for our applications.
Properties Window: In this window, you can edit the properties of the currently selected item/component in the Design Area or Project Window.
Note: If you cannot find the palette or properties window, go to Windows in the menu bar. Select IDE Tools and click on palette or properties.
Adding components to the JFrame is very simple. You have to drag and drop the components you need onto the JFrame from the palette. Simultaneously, you can arrange them in a logical sequence. Then, write the code and run the program.
Running a JFrame Form
You can either click on the green play button on the menu or press shift+F6 to run a JFrame. When the form runs successfully, you will see a window pop-up where you can test your application.