Creating the Login Window


The UserLogin.java file creates the Login window for the Contact List application. Listing 7-1 shows the contents of the UserLogin.java file:

Listing 7-1: The UserLogin.java File

start example
 /*Import required swing classes*/ import javax.swing.*; import javax.swing.text.*; /*Import required awt classes*/ import java.awt.*; import java.awt.event.*; /*Import required io and net classes*/ import java.io.*; import java.net.*; /* class UserLogin - This class is used to create a GUI for user login  Constructor: UserLogin - This constructor creates GUI. Methods: actionPerformed */ public class UserLogin extends JFrame implements ActionListener  {    /*Declares object of Container class.*/    Container contentpane;    /*Declares object of JTextField class.*/    JTextField nametextfield;    JTextField servernametextfield;    /*Declares object of JPasswordField class.*/    JPasswordField passwordtextfield;    /*Declares object of JButton class.*/    JButton submitbutton;    /*Declares object of Socket class.*/    Socket clientsocket;    /*Declares object of String class.*/    String servername;    String username;    String password;    String resource;    String registrationstring="";    /*Declares object of JPanel class.*/    JPanel note;    JPanel combine;    /*Declares object of SocketClass class.*/    SocketClass socketclass;    boolean submitclickd=false;    public UserLogin ()    {       super("Login Window");       this.clientsocket=clientsocket;       contentpane=getContentPane();       /*Initializes the objects of the JTextField class.*/       nametextfield=new JTextField();       servernametextfield=new JTextField();       /*Initializes the object of the JPasswordField class.*/       passwordtextfield=new JPasswordField();       /*Initializes the object of the JPasswordField class.*/       submitbutton=new JButton("Submit");       /*Initializes the object of the JPanel class.*/       combine=new JPanel();         /*Initializes the object of JPanel class.*/       note=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));         /*Initializes the object of the SocketClass class.*/       socketclass=new SocketClass();       /*Sets action command for submitbutton.*/       submitbutton.setActionCommand("submit");       /*Adds action listener for submitbutton.*/       submitbutton.addActionListener(this);       submitbutton.setMnemonic('s');       resource="Home";       /*Declares and initializes the object of JPanel class.*/       JPanel controlpanel=new JPanel();       /*Declares and initializes the objects of JLabel class.*/       JLabel room= new JLabel("User Name");       JLabel name= new JLabel("Password ");       JLabel serverlabel= new JLabel("Server IP");       JLabel firstnode= new JLabel("* If you are not a registerd user your account will be ");       JLabel secondnode= new JLabel("created automatically.");       /*Declares and initializes the object of JPanel class.*/       JPanel lable=new JPanel();       /*Sets GridLayout of controlpanel.*/       controlpanel.setLayout(new GridLayout(4, 2, 3, 3));       controlpanel.setBorder(BorderFactory.createTitledBorder("Login Information"));       /*Adds labels and textfields to controlpanel.*/       controlpanel.add(room);       controlpanel.add(nametextfield);       controlpanel.add(name);       controlpanel.add(passwordtextfield);       controlpanel.add(serverlabel);       controlpanel.add(servernametextfield);       firstnode.setForeground(Color.red);       secondnode.setForeground(Color.red);       /*Adds labels to note.*/       note.add(firstnode);       note.add(secondnode);       /*Declares and initializes the object of JLabel class.*/       JLabel heading=new JLabel("Login Window", JLabel.CENTER);       lable.add(heading,BorderLayout.NORTH);       heading.setFont(new Font("verdana", 1, 12));       contentpane.add(lable,BorderLayout.NORTH);       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       contentpane.add(controlpanel,BorderLayout.CENTER);       /*Declares and initializes the objects of JPanel class.*/       JPanel buttonpanel=new JPanel(new GridLayout(2, 1, 5, 5));       JPanel bp=new JPanel(new FlowLayout());       bp.add(submitbutton);       buttonpanel.add(note);       buttonpanel.add(bp);       contentpane.add(buttonpanel, BorderLayout.SOUTH);       setBounds(5, 5, 300, 240);       submitbutton.addActionListener(this);       show();    }    public void actionPerformed(ActionEvent ae)    {       String actioncommand=ae.getActionCommand();       servername=servernametextfield.getText().trim();       username=nametextfield.getText().trim();       password=passwordtextfield.getText().trim();       /*This block will be executed when an end user clicks the submit button.*/       if (actioncommand.equals("submit"))       {          if (!submitclickd)          {             submitclickd=true;             if (!socketclass.isConnected())             {                socketclass.socketoOpenClass(servername, 5222, 1000);             }             socketclass.setUserNameAndPassword(username,password);             if (socketclass.isConnected()&&(!socketclass.isWaitingForAuth()))             {                socketclass.sendSessionStartMessage();                socketclass.sendAuthorized();                socketclass.setWaitForAuth(true);                socketclass.setUserLoginHandler(this);             }             else             {                JOptionPane.showMessageDialog(null, "Server is not                found.", "wrring", JOptionPane.PLAIN_MESSAGE);             }       }       else       {          submitclickd=false;       }    } } public static void main(String[] args)  { try {    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {    e.printStackTrace(); } /*Declares and initializes the objects of UserLogin class.*/ UserLogin userlogin=new UserLogin(); } } 
end example

Download this listing.

In the above code, the main() method creates an instance of the UserLogin class. The UserLogin class defines the actionPerformed() method. This method acts as an event listener and activates an appropriate method based on the action an end user performs.

The UserLogin.java file creates the Login window of the Contact List application, as shown in Figure 7-2:

this figure shows the login window that allows end users to login to the contact list application.
Figure 7-2: The Login Window




Developing Applications Using Jabber
Developing Applications Using Jabber
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 68

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net