Login Form in Swing
In this example, We will show you sample Java program about, How to create a Login Form in Swing. The example program has been tested and shared in the same post.
Crating Login Form (LoginForm.java)
package com.dineshkrish; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; /** * * @author Dinesh Krishnan * */ public class LoginForm extends JFrame { private static LoginForm loginForm; private JPanel panel; private JLabel userNameLable; private JLabel passwordLabel; private JTextField userNameField; private JPasswordField passwordField; private JButton loginButton; private JButton resetButton; private LoginForm() { } public static LoginForm createLoginForm() { if (loginForm == null) { loginForm = new LoginForm(); } return loginForm; } public void showForm(String title) { // setting the window properties loginForm.setTitle(title); loginForm.setSize(600, 150); loginForm.setVisible(true); loginForm.setDefaultCloseOperation(EXIT_ON_CLOSE); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((screen.getWidth() - this.getWidth()) / 2); int y = (int) ((screen.getHeight() - this.getHeight()) / 2); loginForm.setLocation(x, y); // setting the layout panel = new JPanel(new GridLayout(3, 2)); userNameLable = new JLabel("Username"); userNameField = new JTextField(15); passwordLabel = new JLabel("Password"); passwordField = new JPasswordField(15); // action class LoginAction action = new LoginAction(userNameField, passwordField); resetButton = new JButton("Reset"); resetButton.addActionListener(action); loginButton = new JButton("Login"); loginButton.addActionListener(action); // adding the components to window panel.add(userNameLable); panel.add(userNameField); panel.add(passwordLabel); panel.add(passwordField); panel.add(resetButton); panel.add(loginButton); loginForm.add(panel); } }
Performing Action (LoginAction.java)
package com.dineshkrish; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextField; /** * * @author Dinesh Krishnan * */ public class LoginAction implements ActionListener { private JTextField userNameField; private JTextField passwordField; public LoginAction(JTextField userNameField, JTextField passwordField) { this.userNameField = userNameField; this.passwordField = passwordField; } @Override public void actionPerformed(ActionEvent event) { if ("Login".equals(event.getActionCommand())) { String userName = userNameField.getText(); String password = passwordField.getText(); // your business logic goes here System.out.println("Username :" + userName + ", Password : " + password); } else if ("Reset".equals(event.getActionCommand())) { userNameField.setText(""); passwordField.setText(""); } } }
Main Program (Application.java)
package com.dineshkrish; /** * * @author Dinesh Krishnan * */ public class Application { public static void main(String[] args) { LoginForm form = LoginForm.createLoginForm(); form.showForm("Login"); } }
Output
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet