How to create a login form(Java GUI Programming) through NetBeans IDE or how
to use JPassword?
We will be using a text field to get the username and a password field to get the
password.
1st Method
STEP-1 First of all design your form by placing the required components i.e label,
text-field, password field and a button to perform the action.
Here, I have used a panel with titled border to make design look better.
STEP-2 Double-click on the 'Log In' button or right
clickEventsActionactionPerformed
STEP-3 On the Log In button's action performed event, write the following piece of
code
String user=jTextField1.getText();
String pwd= new String (jPasswordField1.getPassword());
if (user.equals("yourusername") && pwd.equals("yourpassword"))
new Home().setVisible(true);
Explanation of code- We will accept the password from the password field and store
it in the variable pwd. Similarly, we will store the username in the variable user. Now,
we can compare the username and password we have received to the real
username and password using the if command.
Now if the username and password is correct your Home page(Java form) could be
visible or you can perform any other action.
If there is only few IDs i.e only few combinations of username and password you can
use If-else-if but if the IDs are more in number you have to maintain a database of
usernames and their corresponding password. In such a case you can use a DBMS
eg- MySQL. After that you have to create a connection between your database and
your Java application.
found.
2) If you are opening a new form after a login is successful, there will be two forms
on the screen as the login form will not close. So to close the login form just
write dispose(); below the line new Home().setVisible(true); This will
close the current form(i.e login) and there will be only one form on the screen.
Description
Sets the background color of
the password field.
Example
jPasswordField1.setBackground(new
java.awt.Color(255, 255, 204));
setFont
jPasswordField1.setFont(new
java.awt.Font("Times New Roman", 1, 12));
setEnabled
setEchoChar