Creating the Login Window


The Group.java file creates the Login window for the Group Chatting application. Listing 4-1 shows the contents of the GroupLoginGUI.java file:

Listing 4-1: The GroupLoginGUI.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 GroupLoginGUI - This class is used to create a GUI to get the login information from user. Constructor: GroupLoginGUI - This constructor creates GUI. Methods: getUserName - Used to get the user name getPassword - Used to get the password getServarName - Used to get the servername main - This method creates the main window of the application and displays all the components. */ public class GroupLoginGUI extends JFrame implements ActionListener, KeyListener {    /*Declare objects of Container class.*/    Container container=null;    /*Declare objects of JTextField class.*/    JTextField namet=new JTextField();    JTextField server=new JTextField();    /*Declare objects of JPasswordField class.*/    JPasswordField passwordt=new JPasswordField();    /*Declare objects of JButton class.*/    JButton submit=new JButton("Submit");    /*Declare objects of SocketConnection class.*/    Socket clientsocket;    /*Declare string variable.*/    String servername;    String username;    String password;    /*Declare objects of JPanel class.*/    JPanel note=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));      JPanel panel=new JPanel();    JPanel lable=new JPanel();    /*Declare objects of JLabel class.*/    JLabel room= new JLabel("User Name");    JLabel name= new JLabel("Password ");    JLabel serverl= new JLabel("Server IP");    JLabel note1= new JLabel("*If you are not a registered user your account will be");    JLabel note2= new JLabel("created automatically.");    /*Constructor for class.*/    public GroupLoginGUI ()    {       /*Set the window title*/       super("Login Window");       this.clientsocket=clientsocket;       container=getContentPane();       panel.setLayout(new GridLayout(4, 2, 3, 3));       panel.setBorder(BorderFactory.createTitledBorder("Login Information"));       panel.add(room);       panel.add(namet);       panel.add(name);       panel.add(passwordt);       panel.add(serverl);       panel.add(server);       note1.setForeground(Color.red);       note2.setForeground(Color.red);       note.add(note1);       note.add(note2);       JLabel heading=new JLabel("Login Window", JLabel.CENTER);       lable.add(heading,BorderLayout.NORTH);       heading.setFont(new Font("verdana", 1, 12));       container.add(lable,BorderLayout.NORTH);       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       container.add(panel,BorderLayout.CENTER);       JPanel buttonpanel=new JPanel(new GridLayout(2, 1, 5, 5));       JPanel button=new JPanel(new FlowLayout());       button.add(submit);       buttonpanel.add(note);       buttonpanel.add(button);       container.add(buttonpanel,BorderLayout.SOUTH);       setBounds(5, 5, 371, 240);       /*Add the listener to the button*/       submit.addActionListener(this);       submit.addKeyListener(this);       show();    }    public void keyPressed(KeyEvent e)     {       if(e.getKeyCode()==KeyEvent.VK_ENTER)       {       servername=server.getText();       username=namet.getText();       password=passwordt.getText();       GroupList grp=new GroupList(this, servername, username, password);       }    }    public void keyTyped(KeyEvent e)     {    }    public void keyReleased(KeyEvent e)     {    }    public void actionPerformed(ActionEvent e)    {       servername=server.getText();       username=namet.getText();       password=passwordt.getText();       try       {          GroupList grp=new GroupList(this, servername, username, password);          hide();       }       catch(Exception ee)       {       ee.printStackTrace();       }    }    public String getUserName()    {       return username;    }    public String getPassword()    {       return password;    }    public String getServarName()    {       return servername;    }    public static void main(String[] args)     {       /*Set the window look and feel*/       try       {          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());       }       catch(Exception e)       {          e.printStackTrace();       }       GroupLoginGUI lgui=new GroupLoginGUI();    } } 
end example

Download this listing.

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

The methods defined in Listing 4-1 are:

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

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

  • getServerName(): Retrieves the name of the Jabber server.

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

The GroupLoginGUI.java file creates the Login window of the Group Chatting application, as shown in Figure 4-2:

click to expand: this figure shows the login window to log in to the group chatting application.
Figure 4-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