Hello Friends, Today we will learn how to make a simple Login Form in Java Swing With Source Code. A login form or login page is the core functionality of any application. It is usually used to check the authenticity of the user. If the user gives the valid username and password then he/she can log into the system otherwise the system will deny accepting the user’s login request. To create our Gui Login Form or Login Page in Java Swing, we must have good concepts about fundamental components of Java Swing. Below I am providing you all the topics that are required to build our GUI Login Form in Java or we can say as Java code for Login page with Username and Password.
- JFrame JAVA Tutorial For Beginners
- Javax Swing JLabel Tutorial For Beginners
- Javax Swing JTextField Tutorial For Beginners
- Javax Swing JButton Tutorial For Beginners
- Java Swing Password Field -JPasswordField Tutorial
- Java JCheckBox Tutorial
- Java JButton Click Event Tutorial for BeginnersÂ
If you are done with the above topics, then we can start our tutorial login form in Java swing with Source Code.
Also Read –> How to Connect MySQL Database in Java Using Eclipse
Login Form in Java Swing With Source Code | Login Page
Importing Packages
step 1
- To create our Login Page using Java, In the very first step, we need to import all the necessary packages for our program so that we will be able to use all the interfaces and classes that we will use in our program.
1 2 3 4 5 |
//Importing all necessary Packages import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; |
Creating a Class LoginFrame.java
step 2
- In this step, we will create a class (LoginFrame.java) in this example that will both extend the JFrame class and implements the ActionListener interface.
- We are going to implement the ActionListener interface because we will do some click events in our program. ActionListener interface resides in java.awt.event.ActionListener package so, first of all, we have to import this package into our class which we have already done.
- ActionListener contains only one method actionPerformed(ActionEvent e), so if we are implementing the ActionListener interface in any class, then we have to override its method actionPerformed(ActionEvent e) into that class.
- Next, we will create the constructor of the LoginFrame class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //Creating LoginFrame class public class LoginFrame extends JFrame implements ActionListener { //Creating constructor of LoginFrame() class LoginFrame() { } //Overriding actionPerformed() method @Override public void actionPerformed(ActionEvent e) { } } |
Creating Object of LoginFrame class
step 3
- Now we will create another class(Login.java in this example) where our main method will reside.
- Now inside our main method, we will create the object of LoginFrame class and set some of its properties like its title, size and location, default close operation, and its visibility.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFrame extends JFrame implements ActionListener { LoginFrame() { } @Override public void actionPerformed(ActionEvent e) { } } public class Login { public static void main(String[] a){ //Creating object of LoginFrame class and setting some of its properties LoginFrame frame=new LoginFrame(); frame.setTitle("Login Form"); frame.setVisible(true); frame.setBounds(10,10,370,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } } |
- Now save your program compile it and run it.
- You can see that we have created our JFrame. Now we can add components to it.
Setting Layout Manager of JFrame
step 4
- Now inside our LoginFrame class, we will get the content pane of the frame using the getContenPane() method.
- getContentPane() returns a Container reference so we will create a reference to the container.
- Now we are going to set the layout manager of the Container.For this, we are going to create a user-defined method setLayoutManager.
- By default, a content pane uses BorderLayout If we do not like the default layout manager that a content pane uses then we are free to change it to a different one.
- To set the layout manager to null, we have to call the setLayout() method using the object of Container and we will pass null as its argument.By doing this, we are setting the Layout manager of Container to null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFrame extends JFrame implements ActionListener { Container container=getContentPane(); LoginFrame() { //Calling setLayoutManger() method inside the constructor. setLayoutManager(); } public void setLayoutManager() { //Setting layout manager of Container to null container.setLayout(null); } @Override public void actionPerformed(ActionEvent e) { } } public class Login { public static void main(String[] a){ LoginFrame frame=new LoginFrame(); frame.setTitle("Login Form"); frame.setVisible(true); frame.setBounds(10,10,370,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } } |
Adding Components to our JFrame
step 5
- Since we have already created our JFrame, now, we will design our login page in Java by adding components to it.
- To add components to it, first of all, we have to create the objects of all components.
- We are going to create,
- Two JLabel USERNAME and PASSWORD.
- One JTextField to enter username one JPasswordField to enter the password
- Two JButon LOGIN and RESET.
- One JCheckBox showPassword.
- To improve the readability of our program, we are going to create two user-defined methods.First one is setLocationAndSize() and the second one is addComponentsToContainer().
- Now we can set the location and size of the components using setBounds() method and then we can add them to the container using add() method.
- Next, we have to call those user-defined methods inside the constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFrame extends JFrame implements ActionListener { Container container=getContentPane(); JLabel userLabel=new JLabel("USERNAME"); JLabel passwordLabel=new JLabel("PASSWORD"); JTextField userTextField=new JTextField(); JPasswordField passwordField=new JPasswordField(); JButton loginButton=new JButton("LOGIN"); JButton resetButton=new JButton("RESET"); JCheckBox showPassword=new JCheckBox("Show Password"); LoginFrame() { //Calling methods inside constructor. setLayoutManager(); setLocationAndSize(); addComponentsToContainer(); } public void setLayoutManager() { container.setLayout(null); } public void setLocationAndSize() { //Setting location and Size of each components using setBounds() method. userLabel.setBounds(50,150,100,30); passwordLabel.setBounds(50,220,100,30); userTextField.setBounds(150,150,150,30); passwordField.setBounds(150,220,150,30); showPassword.setBounds(150,250,150,30); loginButton.setBounds(50,300,100,30); resetButton.setBounds(200,300,100,30); } public void addComponentsToContainer() { //Adding each components to the Container container.add(userLabel); container.add(passwordLabel); container.add(userTextField); container.add(passwordField); container.add(showPassword); container.add(loginButton); container.add(resetButton); } @Override public void actionPerformed(ActionEvent e) { } } public class Login { public static void main(String[] a){ LoginFrame frame=new LoginFrame(); frame.setTitle("Login Form"); frame.setVisible(true); frame.setBounds(10,10,370,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } } |
- Now save your program, compile it and run it.
- Now we can see that we have successfully added components to JFrame.
- Now the next thing we have to do is to add functionalities to our JButtons and JCheckBox so that when we click on them, they should perform some actions.
Adding Event Handling to JButtons(LOGIN,RESET) and JCheckBox(showPassword)
step 6
- To add event handling to our JButtons and JCheckBox first thing we have to do is to register(add) actionListener to them.
- For this, we will call the addActionListener() method using the object of the desired component.The parameter of the addActionListener() method is the object of that class in which ActionListener interface is implemented and since we are in the same class so we will pass this as the argument of the addActionListener() method.
- By registering the component, the actionPerformed() method will be called whenever we click on any of the registered components.
- Now we will sum up all the things inside a user-defined method addActionEvent().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFrame extends JFrame implements ActionListener { Container container=getContentPane(); JLabel userLabel=new JLabel("USERNAME"); JLabel passwordLabel=new JLabel("PASSWORD"); JTextField userTextField=new JTextField(); JPasswordField passwordField=new JPasswordField(); JButton loginButton=new JButton("LOGIN"); JButton resetButton=new JButton("RESET"); JCheckBox showPassword=new JCheckBox("Show Password"); LoginFrame() { setLayoutManager(); setLocationAndSize(); addComponentsToContainer(); addActionEvent();//calling addActionEvent() method } public void setLayoutManager() { container.setLayout(null); } public void setLocationAndSize() { userLabel.setBounds(50,150,100,30); passwordLabel.setBounds(50,220,100,30); userTextField.setBounds(150,150,150,30); passwordField.setBounds(150,220,150,30); showPassword.setBounds(150,250,150,30); loginButton.setBounds(50,300,100,30); resetButton.setBounds(200,300,100,30); } public void addComponentsToContainer() { container.add(userLabel); container.add(passwordLabel); container.add(userTextField); container.add(passwordField); container.add(showPassword); container.add(loginButton); container.add(resetButton); } public void addActionEvent() { //adding Action listener to components loginButton.addActionListener(this); resetButton.addActionListener(this); showPassword.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { } } public class Login { public static void main(String[] a){ LoginFrame frame=new LoginFrame(); frame.setTitle("Login Form"); frame.setVisible(true); frame.setBounds(10,10,370,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } } |
- The next thing we have to do is to code the desired actions that we want in response whenever we click on the Jbutton or JcheckBox inside actionPerformed() method.
actionPerformed() method
step 7
Now we want that,
- If we enter username as “mehtab” and password as “12345” and click on the LOGIN button then a message dialog box should appear and display the message “Login Successful” and if we enter different username or password then message dialog box should display the message “Invalid Username or Password”.For message dialog box we will use showMessageDialog() method of JOptionPane class.
- If we click on the RESET button, then all the texts of JTextField and JPasswordField should be cleared.
- If we click on the showPassword JCheckBox, then the secret password should be revealed, and if we uncheck the showPassword JCheckBox, then all the texts of the JPasswordField should be masked with “*” characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFrame extends JFrame implements ActionListener { Container container = getContentPane(); JLabel userLabel = new JLabel("USERNAME"); JLabel passwordLabel = new JLabel("PASSWORD"); JTextField userTextField = new JTextField(); JPasswordField passwordField = new JPasswordField(); JButton loginButton = new JButton("LOGIN"); JButton resetButton = new JButton("RESET"); JCheckBox showPassword = new JCheckBox("Show Password"); LoginFrame() { setLayoutManager(); setLocationAndSize(); addComponentsToContainer(); addActionEvent(); } public void setLayoutManager() { container.setLayout(null); } public void setLocationAndSize() { userLabel.setBounds(50, 150, 100, 30); passwordLabel.setBounds(50, 220, 100, 30); userTextField.setBounds(150, 150, 150, 30); passwordField.setBounds(150, 220, 150, 30); showPassword.setBounds(150, 250, 150, 30); loginButton.setBounds(50, 300, 100, 30); resetButton.setBounds(200, 300, 100, 30); } public void addComponentsToContainer() { container.add(userLabel); container.add(passwordLabel); container.add(userTextField); container.add(passwordField); container.add(showPassword); container.add(loginButton); container.add(resetButton); } public void addActionEvent() { loginButton.addActionListener(this); resetButton.addActionListener(this); showPassword.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { //Coding Part of LOGIN button if (e.getSource() == loginButton) { String userText; String pwdText; userText = userTextField.getText(); pwdText = passwordField.getText(); if (userText.equalsIgnoreCase("mehtab") && pwdText.equalsIgnoreCase("12345")) { JOptionPane.showMessageDialog(this, "Login Successful"); } else { JOptionPane.showMessageDialog(this, "Invalid Username or Password"); } } //Coding Part of RESET button if (e.getSource() == resetButton) { userTextField.setText(""); passwordField.setText(""); } //Coding Part of showPassword JCheckBox if (e.getSource() == showPassword) { if (showPassword.isSelected()) { passwordField.setEchoChar((char) 0); } else { passwordField.setEchoChar('*'); } } } } public class Login { public static void main(String[] a) { LoginFrame frame = new LoginFrame(); frame.setTitle("Login Form"); frame.setVisible(true); frame.setBounds(10, 10, 370, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } } |
- Now you can save your program, compile it and run it.
- Now let’s provide username as “mehtab” and password as “12345” and see what happens.
- As we can see the message dialog box has displayed the message “Login Successful”.
- Now let’s enter other username or password.
- As we can see message dialog box has displayed the message “Invalid Username or Password”.
- Now let’s check the JCheckBox showPassword and see what happens.
- As we can see the secret password has been revealed.
- You can also check the RESET button and also uncheck the JCheckBox and it will work fine.
Login Form in Java Swing With Source Code Download
- You can also download the source code of this login system in Java.
- The Download Link of the login page code in Java file is given below..
So this was all for Login Form in Java swing with Source Code tutorial. Feel free to ask if you have any queries regarding this tutorial By commenting on this post. Thank You.
People Are Also Reading…
- Java Swing Login Form with MySQL Database Connection
- How to Create Multi User Login Form in Java using MySQL Database
- How to Play Mp3 File in Java Tutorial | Simple Steps
- Number Guessing Game in Java Swing with Source Code
- Menu Driven Program in Java Using Switch Case
- Calculator Program in Java Swing/JFrame with Source Code
- Registration Form in Java With Database Connectivity
- Tic Tac Toe Game in Java with Source Code
- Text to Speech in Java
- How to Create Splash Screen in Java
- Java Button Click Event
- Best Laptops for Java ProgrammingÂ
- How to Connecty MySQL Database in Java Using Eclipse
- How to Connect MySQL Database in Java using NetBeans
how about using the username and password from ms access data base from your last tutorial about user registration. how does the coding of such?thanks a lot..
You can check my latest post How to connect Ms Access database in Java
Nice explanation…very clear..it helps a lot..thnx for sharing:)
Thank you Midz
am new with java swing application,
if i use jframe to create a form, how can could if insert its data to database
How to set up the database connectivity using Java and Mysql. If I want to validate the login page from backend table
Sir, I am having trouble doing a login form. actually, I am trying to do both registration and login form at the same time. is it ok if they share the same MySQL table? I have 3 columns. the 1st one is unique key and the remaining two are null.
is it possible if you can debug the code for me?
I like the helpful info you provide in your articles. I will bookmark your weblog and check again here frequently. I am quite certain I will learn plenty of new stuff right here! Best of luck for the next!
Future
Thanks for the information.
I would like to know how to do the actual login. Like how you exit the login JFrame and switch to the main JFrame.
Showing a dialog message seems not to complete the login process
Hello Katumba,
Thanks for your comment
I will make an another tutorial in which I will show how to open a new JFrame when we click on the JButton and dispose the one which is already showing up
Thanks and Regards
How to put a background image??
Using JLabel You can put the background Image in JFrame window.
For more information refer these articles
Getting Started With Javax Swing JLabel Tutorial For Beginners
JFrame Tutorial in Java with Examples | Java Swing