Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Login Form in Swing 
 

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

 

                         

» View all related tutorials
Related Tags: c com image class list object diff components stack icons user sed entity icon order component ai define container show

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.