Creating a Chat Room


The RoomInformation.java file allows end users to create a chat room. Listing 3-4 shows the contents of the RoomInformation.java file:

Listing 3-4: The RoomInformation.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.*; /*Imports required io and net package classes*/ import java.io.*; import java.net.*; /* class RoomInformation - This class is used to create a GUI to take the room information from user. Constructor: RoomInformation - This constructor creates GUI. Methods: getRoomName - to get room name  getNickName - to get nick name  */ public class RoomInformation 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 RoomInformation(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)       {          e.printStackTrace();       }       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("Room Information"));       /*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("Create Room", JLabel.CENTER);       /*set the font heading*/       heading.setFont(new Font("verdana", 1, 12));       lable.add(heading, BorderLayout.NORTH);       c.add(lable,BorderLayout.NORTH);       /*set default close operation for window*/       setDefaultCloseOperation(1);       /*set resizable true to show the window*/       setResizable(false);       /*add panel to container*/       c.add(p,BorderLayout.CENTER);       /*Declare objects of JPanel class*/       JPanel buttonpanel=new JPanel(new FlowLayout());       /*add button to container*/       buttonpanel.add(b);       /*Add button panel to container*/       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 Insert 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.setRoomPointer(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 RoomInformation class takes an object of the MainGUI class and the Socket class as input parameters. The RoomInformation class allows end users to invoke the methods of the MainGUI class and the Socket class.

The methods defined in Listing 3-4 are:

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

  • getNickName(): Retrieves the user name of an end user for the created 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 RoomInformation.java file creates the Create Room window that allows end users to create a chat room, as shown in Figure 3-6:

this figure shows the create room window that allows end users to specify a room name and user name of the end user to create a chat room.
Figure 3-6: Creating 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