More Tutorials| Bioinformatics| Open Source| Photoshop| Questions? | Software Development
 

Login Form in SWT

To create a login form in SWT, the Label class set the labels User Name and Password and Text class sets the text for the specified fields.

Login Form in SWT

                         

This section illustrates you how to create Login Form.

To create a login form in SWT, the Label class set the labels User Name and Password and Text class sets the text for the specified fields. The method shell.setLayout(new GridLayout(2, false)) sets the layout of the login form. The method setTextLimit(30) sets the limit on maximum number of characters. The method setEchoCharacter("*")  sets the echo character for the password. A button is created to submit the form. 

After submitting the button, if you haven't specify the username and password, an alert box will be displayed, which is provided by the class MessageBox. If you filled the required fields, the box shows the Welcome message along with the username.

Here is the code LoginForm.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

public class LoginForm {
  Display display = new Display();
  Shell shell = new Shell(display);
  Label label1,label2;
  Text username;
  Text password;
  Text text;
 
    public LoginForm() {
    shell.setLayout(new GridLayout(2false));
  shell.setText("Login form");
    label1=new Label(shell, SWT.NULL);
  label1.setText("User Name: ");
    
    username = new Text(shell, SWT.SINGLE | SWT.BORDER);
    username.setText("");
    username.setTextLimit(30);
    
    label2=new Label(shell, SWT.NULL);
  label2.setText("Password: ");
    
    password = new Text(shell, SWT.SINGLE | SWT.BORDER);
    System.out.println(password.getEchoChar());
    password.setEchoChar('*');
    password.setTextLimit(30);

    Button button=new Button(shell,SWT.PUSH);
  button.setText("Submit");
  button.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
      String selected=username.getText();
          String selected1=password.getText();
      
    if(selected==""){ 
      MessageBox messageBox = new MessageBox(shell, SWT.OK |
            SWT.ICON_WARNING |SWT.CANCEL);

      messageBox.setMessage("Enter the User Name");
       messageBox.open();
    }
    if(selected1==""){
      MessageBox messageBox = new MessageBox(shell, SWT.OK |
             SWT.ICON_WARNING |SWT.CANCEL);

      messageBox.setMessage("Enter the Password");
      messageBox.open();
    }
    else{
     MessageBox messageBox=new MessageBox(shell,SWT.OK|SWT.CANCEL);
     messageBox.setText("Login Form");
     messageBox.setMessage("Welcome:" + username.getText());
     messageBox.open();
    }
    }
  });
    username.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    password.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    
    shell.pack();
    shell.open();
    
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
   display.dispose();
  }
   public static void main(String[] args) {
   new LoginForm();
  }
}

Output will be displayed as:

After submitting the form, following message will be displayed in the message box:

Download Source Code

 

                         

» View all related tutorials
Related Tags: c class list orm text select form console button io swt size method sed display sets toolbar ole this location

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 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
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

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

Copyright © 2008. All rights reserved.