Build first GUI Application in Java using NetBeans IDE

This tutorial is an introduction to GUI applications in Java using NetBeans IDE.

First GUI Application 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.

You can check this: GUI Application to Calculate Area of a Square in NetBeans IDE using Java

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

  1. Choose File > Select New Project. Alternatively, you can click the New Project icon in the toolbar.
  2. Create new project NetBeans IDE in JavaA window will show up. Select Java from Categories pane and Java Application from Projects pane. Click Next.
  3. New name to NetBeans IDE in JavaEnter the desired name for your project in the Project Name field. By default, NetBeans saves your project in the NetBeansProjects folder in My Documents.
  4. You can uncheck the Create Main Class field.
  5. 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

  1. Creating a New Form in NetBeans IDE - JavaGo to the projects window. Then right-click on the project that you created and choose New > JFrame Form.
  2. Creating a New Form in NetBeans IDE - JavaEnter the name of the form in the Class Name field and the project name in the Package field.
  3. Click Finish.

After creating a new form, you will see an interface of this kind as shown below:
Add new form in NetBeans Java

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
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.

Leave a Reply

Your email address will not be published. Required fields are marked *