Joining a Chat Room


The ChatRoom.java file creates the user interface to enter into an existing chat room to initiate a chat. Listing 3-3 shows the contents of the ChatRoom.java file:

Listing 3-3: The ChatRoom.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 ChatRoom-This class is used to create a GUI to take the room name and nick from user. Constructor: ChatRoom-This constructor creates GUI. Methods: getRoomName-to get room name  getNickName-to get nick name  */ public class ChatRoom extends JDialog implements ActionListener {    /*Declare objects of Container class*/    Container c=null;    /*Declare objects of JTextField class*/    JTextField roomt=new JTextField();    JTextField namet=new JTextField();    /*Declare objects of JButton class*/    JButton b=new JButton("Submit");    /*Declare objects of MainGUI class*/    MainGUI maingui;    /*Declare objects of Socket class*/    Socket clientsocket;    static Socket clientsocket1;    /*Declare String variables*/    String servername="localhost";    String password="user4";    String resource="Home";    String  recerver;    String roomname;    String nickname;    String username;    /*Declare objects of MutableAttributeSet class*/    MutableAttributeSet sendernameattrib;    /*Constructor for class*/    public ChatRoom(Socket clientsocket, MainGUI maingui)    {       setTitle("Chat Room Application");       sendernameattrib=new SimpleAttributeSet();       StyleConstants.setFontFamily(sendernameattrib,"Verdana");       StyleConstants.setForeground(sendernameattrib,Color.blue);       try       {          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());       }       catch(Exception e)       {       }       this.clientsocket =clientsocket;       c=getContentPane();       this.maingui=maingui;       /*Declare objects of JPanel class*/       JPanel p = new JPanel();       JLabel room = new JLabel("Room Name");       JLabel name = new JLabel("User Name");       JPanel lable = new JPanel();       /*set the layout*/       p.setLayout(new GridLayout(3, 2, 10, 10));       /*set the border to panel*/       p.setBorder(BorderFactory.createTitledBorder("Chat Room"));       /*add the component to container*/       p.add(room);       p.add(roomt);       p.add(name);       p.add(namet);       /*Declare objects of JLabel class*/       JLabel heading=new JLabel("Chat Room", JLabel.CENTER);       /*set the font heading*/       heading.setFont(new Font("verdana", 1, 12));       lable.add(heading, BorderLayout.NORTH);       /*add panel to container*/       c.add(lable,BorderLayout.NORTH);       /*set default close operation for window*/       setDefaultCloseOperation(1);       /* set resizable true to show the window*/       setResizable(false);       c.add(p,BorderLayout.CENTER);       /*Declare objects of JPanel class*/       JPanel buttonpanel=new JPanel(new FlowLayout());       /*add button to container*/       buttonpanel.add(b);       c.add(buttonpanel,BorderLayout.SOUTH);       /*set bounds for window*/       setBounds(5, 5, 300, 200);       /*add action listener to button*/       b.addActionListener(this);       /*calls the show function to show the window*/       show();    }    /*function to handle the event*/    public void actionPerformed(ActionEvent e)    {       if((JButton)e.getSource()==b)       {          if(roomt.getText()==null||roomt.getText().trim().equals(""))          {             JOptionPane.showMessageDialog(this, "Please specify the room name." );             return;          }          if(namet.getText()==null||namet.getText().trim().equals(""))          {             JOptionPane.showMessageDialog(this, "Please specify the user name." );             return;          }          String dialog=null;          roomname=roomt.getText();          nickname=namet.getText();          this.username=maingui.username;          String roomString="<presence from=\""+username+"@conference.localhost/Home\"          to=\""+roomname.trim()+"@conference."+servername+"/"+nickname.trim()+"\"/>";          SocketOpener.sendXMLToJabber(roomString);          SocketOpener.setChatPointer(this, maingui);          return;       }    }    /*function to get the room name*/    public String getRoomName()    {       return roomname;    }    /*function to get the nick name*/    public String getNickName()    {       return nickname;    } } 
end example

Download this listing.

In the above code, the constructor of the ChatRoom class takes an object of the MainGUI class and the Socket class as input parameters. The ChatRoom class allows end users to invoke the methods of the MainGUI class and the Socket class.

The methods defined in Listing 3-3 are:

  • getRoomName(): Retrieves the name of the chat room specified by an end user.

  • getNickName(): Retrieves the user name of an end user for the selected chat room.

  • 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 user interface.

The ChatRoom.java file creates the Chat Room window that allows end users to participate in a chat, as shown in Figure 3-5:

this figure shows the chat room window that allows end users to specify the chat room name and the user name to join a chat room.
Figure 3-5: Joining a Chat Room




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