Creating the Login Window


The UserLogin.java file creates the user interface to log on as an existing user for the Connector application. Listing 2-3 shows the contents of the UserLogin.java file:

Listing 2-3: 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 - This method is called when end user clicks on send button. */ public class UserLogin extends JDialog implements ActionListener   {       Container contentpane;       /*       Declares object of JPasswordField class.       */       JPasswordField passwordtextfield;       /*        Declares objects of JTextField class.       */       JTextField nametextfield;       JTextField servernametextfield;       /*Declares object of JButton class.*/       JButton submitbutton;       /*Declares objects of String class.*/       String servername;       String username;       String password;       String resource;       String registrationstring="";       /*Declares objects of JPanel class.*/       JPanel note;       JPanel combine;       /*       Declares objects of JabberClient class.       */       JabberClient jabberclient;       /*       Declares objects of SocketClass class.       */       SocketClass socketclass;       boolean submitclickd=false;       public UserLogin (SocketClass socketclass,JabberClient jabberhandler)       {          setTitle("Login Window");          this.socketclass=socketclass;          contentpane=getContentPane();          jabberclient=jabberhandler;          addWindowListener(new WindowAdapter()          {             public void windowClosing(WindowEvent we)             {                setVisible(false);             }          });          /*           Initializes the objects of JTextField class.          */          nametextfield=new JTextField();          servernametextfield=new JTextField();          /*           Initializes the object of JPasswordField class.          */          passwordtextfield=new JPasswordField();          /*          Initializes the object of JButton class.          */          submitbutton=new JButton("Submit");          /*           Initializes the objects of JPanel class.          */          combine=new JPanel();            note=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));            submitbutton.setActionCommand("submit");          submitbutton.addActionListener(this);          submitbutton.setMnemonic('s');          resource="Home";          /*          Declares and Initializes the objects of JPanel class.          */          JPanel controlpanel=new JPanel();          JPanel lablepanel=new JPanel();                /*          Declares and Initializes the objects of JLabel class.          */          JLabel room= new JLabel("User Name");          JLabel name= new JLabel("Password ");          /*          Sets the GridLayout of the controlpanel.          */          controlpanel.setLayout(new GridLayout(4, 2, 3, 3));          controlpanel.setBorder(BorderFactory.createTitledBorder("Login Information"));               /*           Adds the labels and textfields to controlpanel.           */          controlpanel.add(room);          controlpanel.add(nametextfield);          controlpanel.add(name);          controlpanel.add(passwordtextfield);               controlpanel.add(serverl);          controlpanel.add(servernametextfield);          /*          Declares and Initializes the objects of JLabel class.          */               JLabel heading=new JLabel("Login Window",JLabel.CENTER);          /*Adds the heading to lablepanel.*/          lablepanel.add(heading,BorderLayout.NORTH);          /*Sets the font to the heading.*/          heading.setFont(new Font("verdana", 1, 12));          /*Adds the lablepanel to contentpane.*/          contentpane.add(lablepanel,BorderLayout.NORTH);          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);          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           setBounds(5, 5, 300, 240);          submitbutton.addActionListener(this); show();          getRootPane().setDefaultButton(submitbutton);         }            public void actionPerformed(ActionEvent ae)        {          String actioncommand=ae.getActionCommand();          servername=servernametextfield.getText().trim();          username=nametextfield.getText().trim();          password=passwordtextfield.getText().trim();          if (username.equals(""))          {             JOptionPane.showMessageDialog(null, "User Name is a required             field.", "Error", JOptionPane.PLAIN_MESSAGE);             return;          }          if (password.equals(""))          {             JOptionPane.showMessageDialog(null,"Password is a required             field.","Error",JOptionPane.PLAIN_MESSAGE);             return;          }          if (actioncommand.equals("submit"))          {             String sessionstart="";             if (!submitclickd)             {                submitclickd=true;                if (servername.equals(""))                { JOptionPane.showMessageDialog(null, "Server name is a required                field.","Error",JOptionPane.PLAIN_MESSAGE);                return;             }             if (jabberclient.getServerName().equals(servername))             {             }             else             {                if (socketclass.isConnected)                {                   socketclass.closeConnection();                }                socketclass.socketoOpenClass(servername, 5222, 1000);                if ((!socketclass.isConnected))                {                   setVisible(false);                        return;                }             }             if (jabberclient.getStatus().equals("notconnected")||jabberclient.getStatus().             equals("waitforreg")||jabberclient.getStatus().equals("waitforauth"))             {                sessionstart=socketclass.sendSessionStartMessage();             }             jabberclient.setUserNameAndPassword(username,password,servername);             socketclass.setUserNameAndPassword(username,password,servername);             String authstring=socketclass.sendAuthorized();             jabberclient.setMessageInToTextBox(sessionstart+"\n"+authstring);             jabberclient.setStatus("waitforauth");             setVisible(false);             }             else             {                submitclickd=false;             }          }       }         } 
end example

Download this listing.

In the above code, the constructor of the UserLogin class takes an object of the SocketClass class and an object of the JabberClient class as input parameters. This allows an end user to invoke the methods of the SocketClass and JabberClient classes. The UserLogin class defines the actionPerformed() method. The actionPerformed() method acts as an event listener and activates an appropriate method, based on the action an end user performs. End users invoke the actionPerformed() method by selecting any option from the User Login window.

Select File-> Login to log on as an existing end user. The UserLogin.java file generates the Login window, as shown in Figure 2-7:

this figure shows the login window to log on to the connector application.
Figure 2-7: 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