In this article, we will be learning how to create a login form in Java using NetBeans.
Installation of NetBeans
The Java SE Development Kit(JDK) 7 or JDK 8 is required to install the NetBeans IDE.
Now, download the latest NetBeans IDE from its official channel. Download the whole IDE bundle version depending upon your processor architecture. The user can add or remove the packs using the IDE’s Plugin Manager. You can check out the steps to install and use NetBeans in this article.
Assume that you have successfully installed NetBeans on your machine. Start the NetBeans from the Window. The NetBeans main window will appear as shown below.
The NetBeans main window contains menus, toolbars, the project pane, the files pane, the runtime pane, the navigator pane, and other panes. The NetBeans main window is the command center for the IDE.
Creating a blank Project
To create and run a program, you have to first create a project.
Here are the steps to create a demo project
Step 1: – Choose File and New Project to display the New Project dialog box.
The new project box enables you to specify the project type.
Step 2: – Choose the Java type from the given list.
Step 3: – From the New Application Tab, enter the project name and unselect the main class from it.
These steps are to be followed.
Step 1: – Create a blank Java project with the name loginpageclassy. Uncheck the Main class option from the project tab. Note down the address of the directory and download any image for a background and login label and for the header a shown below.
Step2: – To add a JFrame, right-click the project name in the Project window.
Step 3: – Now right-click the JFrame, click Set Layout and select Null Layout from the option.
Step 4: – Open the project folder and drag those images into the project field as shown below.
Step5: – Now drag a label field from the palette. Right-click on the Jlabel button and select icons, where you can select the image for that particular label. Make this label the background of the project by extending it to all the four corners of the JFrame.
Step6: – Right-click on the label field to enter into the Properties and select the icon to set the custom icon for the label. Make this label the background of the project by extending it to all the four corners of the JFrame.
Step7: – Now drag a Text Field and a Password Field from the Pallette into the JFrame. Now, Drag three more labels into the JFrame for labeling the Username, Password and for the Login button.
Step8: – Set the Text field and the Password field to empty. Rename the label for the Username and the Password. Therefore the whole form appears as below.
Step9: – Now, Right-click on to the Login button and select Events >>Mouse >>Mouse Click.
This will direct it to the Source section.
A string ‘ext’ is declared in order to get the text entered into the Password field. The text entered in the text field and the password field is compared with the Username and the password, which has been set already. If the text entered in both the fields match with the previously saved ones, the user gets a pop-up message showing that the Login was successful, else a Login Failed message will appear.
Right-click on to the Login button and select Events >>Mouse >>Mouse Click.
This will direct to the below statement in the Source section.
private void jLabel3MouseClicked(java.awt.event.MouseEvent evt)
Declaration of string and coding for the comparing Username and Password.
The diagram below lists the purpose of each statement used in this logic.
Now, Save the program and click the Run button.
The Login form will be shown. Enter the Username and Password correctly. This gives a message tab showing that the login was successful.
If the Username or the password entered is wrong, the screen shown below appears.
Now that you have successfully learned to build an impressive login page, you can also try making other projects such as creating a basic calculator in Java using Netbeans. Also, if you want to build more projects in Java as a beginner, try “Learn Projects in Java for Beginners from Scratch” online course absolutely FREE!
We hope that this article has helped you in understanding the basics of creating a Login Form. Feel free to contact us in case if you face any issues regarding the same.
People Are Also Reading:
- 10 Ways to Learn Java in just a Couple of Weeks
- Is It Possible to Use Java with iOS?
- Docker for Java Development Environment With Ubuntu