Creating the Login Window


The LoginGUI.java file creates the Login window for the Chat Room application. Listing 3-1 shows the contents of the LoginGUI.java file:

Listing 3-1: The LoginGUI.java File

start example
 /*Imports required swing package classes*/ import javax.swing.*; import javax.swing.text.*; /*Imports required awt package classes*/ import java.awt.*; import java.awt.event.*; /* Import required io and net classes */ import java.io.*; import java.net.*; /* class LoginGUI - This class is used to create a GUI to get the information from user to login . Constructor: LoginGUI - This constructor creates GUI. Methods: getUserName:get the user name getServerName:get the server name */ public class LoginGUI extends JFrame implements ActionListener, KeyListener {    /*Declare objects of Container class.*/    Container c=null;    /*Declare objects of JTextField class.*/    JTextField namet=new JTextField();    JTextField server=new JTextField();    JPasswordField passwordt=new JPasswordField();    /*Declare objects of JButton class.*/    JButton b=new JButton("Submit");    /*Declare objects of Socket class.*/    Socket clientsocket;    /*Declare String variable*/    String servername;    String username;    String password;    String resource="Home";    String  recerver;    String roomname;    String nickname;    String registrationstring="";    String servererror="";    /*    Declare objects of MutableAttributeSet class.    */    MutableAttributeSet sendernameattrib;    /*Declare objects of JPanel class.*/    JPanel note=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));      JPanel combine=new JPanel();      /* Constructor for class*/    public LoginGUI ()    {       /*set the title for window*/       super("Chat Room Application");       sendernameattrib=new SimpleAttributeSet();       StyleConstants.setFontFamily(sendernameattrib,"Verdana");       StyleConstants.setForeground(sendernameattrib,Color.blue);       this.clientsocket=clientsocket;       c=getContentPane();       /*Declare objects of JPanel class.*/       JPanel p=new JPanel();       JLabel room= new JLabel("User Name");       JLabel name= new JLabel("Password");       JLabel serverl= new JLabel("Server IP");       JLabel note11= new JLabel("* If you are not a registered user then, your account will be created       automatically.");       JLabel note1= new JLabel("* If you are not a registerd user your account will be");       JLabel note2= new JLabel("created automatically.");       JPanel lable=new JPanel();       /*set the layout for window*/       p.setLayout(new GridLayout(4, 2, 3, 3));       /*set the border for user*/       p.setBorder(BorderFactory.createTitledBorder("Login Information"));       /*Add the component in container*/       p.add(room);       p.add(namet);       p.add(name);       p.add(passwordt);       p.add(serverl);       p.add(server);       /*set the color for label*/       note1.setForeground(Color.red);       note2.setForeground(Color.red);       /*add the label to container*/       note.add(note1);       note.add(note2);       /*Declare objects of JLabel class.*/       JLabel heading=new JLabel("Login Window", JLabel.CENTER);       lable.add(heading,BorderLayout.NORTH);       /*set the heading for heading*/       heading.setFont(new Font("verdana", 1, 12));       c.add(lable,BorderLayout.NORTH);       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       c.add(p,BorderLayout.CENTER);       /*Declare objects of JPanel class.*/       JPanel buttonpanel=new JPanel(new GridLayout(2, 1, 5, 5));       JPanel bp=new JPanel(new FlowLayout());       bp.add(b);       buttonpanel.add(note);       buttonpanel.add(bp);       c.add(buttonpanel,BorderLayout.SOUTH);       /*set the bounds for window*/       setBounds(5, 5, 371, 240);       /*add the listener to button*/       b.addActionListener(this);       b.addKeyListener(this);       /*called the function to show the window*/       show();    }    /*function to handle the key event */    public void keyPressed(KeyEvent e)     {       if(e.getKeyCode()==KeyEvent.VK_ENTER)       {          servername=server.getText();          username=namet.getText();          password=passwordt.getText();          MainGUI mn=new MainGUI(username,password,servername,this);       }    }    /*function to handle the key event*/    public void keyTyped(KeyEvent e)     {    }    /*function to handle the key event*/    public void keyReleased(KeyEvent e)     {       if(e.getKeyCode()==KeyEvent.VK_ENTER)       {          servername=server.getText();          username=namet.getText();          password=passwordt.getText();          MainGUI mn=new MainGUI(username,password,servername,this);          servererror=mn.servererror;       }    }    /* function to handle the  event*/    public void actionPerformed(ActionEvent e)    {       servername=server.getText();       username=namet.getText();       password=passwordt.getText();       try       {          MainGUI mn=new MainGUI(username,password,servername,this);       }       catch(Exception ee)       {          System.out.println("Server not found ::: "+ee);       }    }    /*function to get username*/    public String getUserName()    {       return username;    }    /*function to get password*/    public String getPassword()    {       return password;    }    /* function to get servername*/    public String getServarName()    {       return servername;    }    public static void main(String[] args)     {       /*        Initialize and set the Look and Feel of the application to Windows Look and Feel.       */       try       {          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());       }       catch(Exception e)       {          e.printStackTrace();       }       LoginGUI lgui=new LoginGUI();    } } 
end example

Download this listing.

In the above code, the main() method creates an instance of the LoginGUI class.

The methods defined in Listing 3-1 are:

  • getUserName(): Retrieves the name of the end user currently logged in.

  • getServerName(): Retrieves the name of the Jabber server specified by an end user.

  • getPassword(): Retrieves the password specified by an end user.

  • actionPerformed(): Acts as an event listener and activates an appropriate method based on the action an end user performs. End users invoke this method by clicking any button on the Login window.

The LoginGUI.java file creates the Login window of the Chat Room application, as shown in Figure 3-2:

click to expand: this figure shows the login window to login to the chat room application.
Figure 3-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