Home Java Example Java Swing Login Form in Swing
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Login Form in Swing

Advertisement
In the LoginDemo.java, we have create two text fields text1 and text2 to set the text for username and password. A button is created to perform an action.

Login Form in Swing

     

This section illustrates you how to create a Login form.

To create a Login Form, we have used two class files:
1) NextPage.java
2) LoginDemo.java

In the LoginDemo.java, we have create two text fields text1 and text2 to set the text for username and password. A button is created to perform an action. The method text1.getText() get the text of username and the method  text2.getText() get the text of password which the user enters. Then we have create a condition that if the value of text1 and text2 is roseindia, the user will enter into the next page on clicking the submit button. The NextPage.java is created to move the user to the next page. In case if the user enters the invalid username and password , the class JOptionPane provides the MessageDialog to shows the error message.

Here is the code of NextPage.java

import javax.swing.*;
import java.awt.*;
 
class NextPage extends JFrame
{
  NextPage()
 {
 setDefaultCloseOperation(javax.swing.
  WindowConstants.DISPOSE_ON_CLOSE);
 setTitle("Welcome");
 setSize(400, 200);
  }
 }

Here is the code of LoginDemo.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
class Login extends JFrame implements ActionListener
{
 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
  Login()
  {
  label1 = new JLabel();
  label1.setText("Username:");
  text1 = new JTextField(15);

  label2 = new JLabel();
  label2.setText("Password:");
  text2 = new JPasswordField(15);
 
  SUBMIT=new JButton("SUBMIT");
  
  panel=new JPanel(new GridLayout(3,1));
  panel.add(label1);
  panel.add(text1);
  panel.add(label2);
  panel.add(text2);
  panel.add(SUBMIT);
  add(panel,BorderLayout.CENTER);
  SUBMIT.addActionListener(this);
  setTitle("LOGIN FORM");
  }
 public void actionPerformed(ActionEvent ae)
  {
  String value1=text1.getText();
  String value2=text2.getText();
  if (value1.equals("roseindia") && value2.equals("roseindia")) {
  NextPage page=new NextPage();
  page.setVisible(true);
  JLabel label = new JLabel("Welcome:"+value1);
  page.getContentPane().add(label);
  }
  else{
  System.out.println("enter the valid username and password");
  JOptionPane.showMessageDialog(this,"Incorrect login or password",
  "Error",JOptionPane.ERROR_MESSAGE);
  }
}
}
 class LoginDemo
{
  public static void main(String arg[])
  {
  try
  {
  Login frame=new Login();
  frame.setSize(300,100);
  frame.setVisible(true);
  }
  catch(Exception e)
  {JOptionPane.showMessageDialog(null, e.getMessage());}
  }
}

Output will be displayed as:

 

Download Source Code

 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 11, 2008

Ask Questions?    Discuss: Login Form in Swing   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Amir
April 2, 2011
Java

so....if i want to connect a database using this coding....how????
rajni
June 8, 2011
ques

password field will not get the astrik using dis code,how to convert d string of password to astrik form
saiteja
July 4, 2011
it shows the problems as

E:\frames>javac SFrame.java SFrame.java:4: SFrame is not abstract and does not override abstract method acti onPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener class SFrame extends JFrame implements ActionListener ^ 1 error E:\frames>
anitha
August 25, 2011
code

good
luthando
September 6, 2011
IT

I'm stragling to use quiries using java
khem
September 15, 2011
JAVA

i want code of login page, and i feel very relax
ravi pali
October 25, 2011
login form

my login formimport javax.swing.*; import java.awt.*; class NextPage extends JFrame { NextPage() { setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE); setTitle("Welcome"); setSize(400, 200); } } Here is the code of LoginDemo.java import javax.swing.*; import java.awt.*; import java.awt.event.*; class Login extends JFrame implements ActionListener { JButton SUBMIT; JPanel panel; JLabel label1,label2; final JTextField text1,text2; Login() { label1 = new JLabel(); label1.setText("Username:"); text1 = new JTextField(15); label2 = new JLabel(); label2.setText("Password:"); text2 = new JPasswordField(15); SUBMIT=new JButton("SUBMIT"); panel=new JPanel(new GridLayout(3,1)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(SUBMIT); add(panel,BorderLayout.CENTER); SUBMIT.addActionListener(this); setTitle("LOGIN FORM"); } public void actionPerformed(ActionEvent ae) { String value1=text1.getText(); String value2=text2.getText(); if (value1.equals("roseindia") && value2.equals("roseindia")) { NextPage page=new NextPage(); page.setVisible(true); JLabel label = new JLabel("Welcome:"+value1); page.getContentPane().add(label); } else{ System.out.println("enter the valid username and password"); JOptionPane.showMessageDialog(this,"Incorrect login or password", "Error",JOptionPane.ERROR_MESSAGE); } } } class LoginDemo { public static void main(String arg[]) { try { Login frame=new Login(); frame.setSize(300,100); frame.setVisible(true); } catch(Exception e) {JOptionPane.showMessageDialog(null, e.getMessage());} } }
ravi
November 4, 2011
login form

my login form
ferat
December 2, 2011
database connection

allright how can we do database connection for users.example : there is a database which includes usernames and passwords.
murtaza
December 6, 2011
murtaza

yahoo jani and i love yahoo
akhilesh kumar choudhary
January 5, 2012
regarding project on hotel management system

sir, please provide me code java project on hotel management system with have no advance booking criteria, or provide me detail from where i shall get it with free.
Devendran
January 7, 2012
post

i'm fresher to swing concept....thank you
Avinash
January 16, 2013
java

login form
saagr vekariya
March 30, 2012
jframe

thanks
UMAR RIZWAN
April 1, 2012
english

best
Shanka
April 15, 2012
java, login with "Fogot password"

pls send me codings... :)
rajesh
July 5, 2012
good job

thanks for this example
gopal
August 20, 2012
java

how are you
Thaya
August 21, 2012
swing java

Thank You so much for creating this login form program.. it is very easy to study.
hamood
November 19, 2012
cinma ticket

plese i want code for cinma ticket app
sourabh
November 21, 2012
........

nice...............
Kudakwashe Muparaganda
November 30, 2012
Login Form

I am new to JAVA. I would like to know how to invoke login form
saravanan
November 18, 2013
about this prog

when the error window appear it shows the ok button. u did,t decalare any ok button in this prog how its possible
appu
July 13, 2012
date field in java swing form

please help to solve my problem i want to add a date field in may project form.. example reservation form how to add the date of jurney date of birth..etc
DMCA.com