Swing login form example

In this example we have described swing login form. We have created one text field "textField" and one jPasswordField and we have set text for username and password. We create one submit button and perform an action.

Swing login form example

Swing login form example

In this example we have described swing login form. We have created one text field "textField" and one jPasswordField and we have set text for username and password. We create one submit button and perform an action. We have used get method. get method has get value username and password. Then we create a condition that if "username" textField and "password" jPasswordField set as naulej matches then you will be redirected to the next page after clicking on submit button. If you will enter the wrong username and password, an error message will be displayed for which we have used JOptionPane and provided the MessageDialog box.

Examples

LoginExample.java

import javax.swing.*;

import java.awt.event.*;

public class LoginExample extends JFrame {

	public static void main(String[] args) {
		LoginExample frame = new LoginExample();
	}

	JButton button = new JButton("Submit");
	JPanel panel = new JPanel();
	JTextField textField = new JTextField(15);
	JPasswordField jPasswordField = new JPasswordField(15);
	JLabel label = new JLabel();
	JLabel label2 = new JLabel();
	LoginExample() {
		
		super("Autentification");
		label.setText("Username:");
		label2.setText("Password:");
		
		setSize(300, 200);
		setLocation(500, 280);
		panel.setLayout(null);

		
		
		label.setBounds(10, 30, 100, 20);
		label2.setBounds(10,65,  100,  20);
		textField.setBounds(80, 30, 150, 20);
		jPasswordField.setBounds(80, 65, 150, 20);
		button.setBounds(110, 100, 80, 20);
        
		
		
		panel.add(label);
		panel.add(label2);
		panel.add(button);
		panel.add(textField);
		panel.add(jPasswordField);

		getContentPane().add(panel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		actionlogin();
	}

	public void actionlogin() {
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String name = textField.getText();
				String pwd = jPasswordField.getText();
				if (name.equals("naulej") && pwd.equals("naulej")) {
					newframe regFace = new newframe();
					regFace.setVisible(true);
					dispose();
				} else {

					JOptionPane.showMessageDialog(null,
							"Wrong Password / Username");
					textField.setText("");
					jPasswordField.setText("");
					textField.requestFocus();
				}

			}
		});
	}
}

newframe.java

import javax.swing.*;

public class newframe extends JFrame {

	public static void main(String[] args) {
		newframe frameTabel = new newframe();
	}

	JLabel welcome = new JLabel("Welcome to Roseindia.net");
	JPanel panel = new JPanel();

	newframe() {
		  setSize(300, 200);
		  setLocation(500, 280);
		  panel.setLayout(null);

		welcome.setBounds(70, 50, 150, 60);

		panel.add(welcome);

		getContentPane().add(panel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

}

Output

Download Source Code