Initiating a Group Chat


The GroupChat.java file creates the user interface that allows end users to participate in a group chat. Listing 4-6 shows the contents of the GroupChat.java file:

Listing 4-6: The GroupChat.java File

start example
 /*Import required swing classes*/ import javax.swing.event.*; import javax.swing.text.*; import javax.swing.*; /*Import required awt classes*/ import java.awt.*; import java.awt.event.*; /*Import required util classes*/ import java.util.*; /* class GroupChat - This class is used to create a GUI for chatting. Constructor: GroupChat-This constructor creates GUI. Methods: clearPane - Used to clear the text addElement - Used to add user to the list writeintotextfield-Used to write text into textpane */ public class  GroupChat extends JFrame implements ActionListener {    /*Declare object of Container class.*/    Container cont = null;    /*Declare object of JPanel class.*/    JPanel jp=new JPanel(new BorderLayout());    /*Declare object of JButton class.*/    JButton button=new JButton("Send");    /*Declare object of JTextField class.*/    JTextField text=new JTextField();    /*Declare object of JList class.*/    JList list;    /*Declare object of DefaultListModel class.*/    private DefaultListModel listModel;    /*Declare object of JTextPane class.*/    JTextPane  je=new JTextPane();    /*Declare object of JScrollPane class.*/    JScrollPane scrollPane1 = new JScrollPane(je);    JScrollPane scrollPane ;    /*Declare object of JSplitPane class.*/    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);    /*Declare the string variable*/    String chatText=null;    String roomname;    String user;    String username="";    String servername;    /*Declare object of Document class.*/    Document contentmodel;    /*Declare object of MutableAttributeSet class.*/    MutableAttributeSet nicknameattrib;    SocketConnection sc;    /*Used to clear the text*/    public void clearPane()    {       je.setText("");    }    public void addElement(String element)    {       listModel.addElement(element);    }    /*Constructor for class*/    public GroupChat(String user, String servername, String roomname, SocketConnection clientsocket)    {       /*Set the title for window*/       super(" Group Chatting Application: "+roomname);       cont = getContentPane();       /*Set the textpane noneditable*/       je.setEditable(false);       /*Set the attribute for the font*/       nicknameattrib=new SimpleAttributeSet();       StyleConstants.setFontFamily(nicknameattrib, "Verdana");       StyleConstants.setForeground(nicknameattrib, Color.black);       contentmodel=je.getDocument();       this.roomname=roomname;       this.user=user;       this.servername=servername;       /*Create the instance of DefaultListModel*/       listModel=new DefaultListModel();       String groupmessage="<presence from=\""+user+"@conference.localhost/Home\"        to=\""+roomname.trim()+"@conference."+servername+"/"+user.trim()+"\"/>";       sc=clientsocket;       sc=sc.getSocketClass();       sc.setGroupuser(this);       /*Called the sendXMLToJabber function to send the message to jabber server*/       sc.sendXMLToJabber(groupmessage);       /*Add the listModel in to List */       list=new JList(listModel);       /*Add the list into JScrollPane*/       scrollPane= new JScrollPane(list);       list.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);       list.setVisibleRowCount(5);       /*Set the scrollpane into splitpane*/       splitPane.setTopComponent(scrollPane1);       splitPane.setBottomComponent(scrollPane);       /*Set the divider of splitpane*/       splitPane.setDividerLocation(400);        splitPane.setPreferredSize(new Dimension(700, 200));       list.setSelectionBackground(Color.cyan);       /*Set the size of button*/       button.setPreferredSize(new Dimension(130, 20));       /*Set the size of textfield*/       text.setPreferredSize(new Dimension(400, 20));       /*Add the text field in to panel*/       jp.add(text,BorderLayout.WEST);       /*Add the button field in to panel*/       jp.add(button,BorderLayout.EAST);       /*Add the panel in to container*/       cont.add(jp,BorderLayout.SOUTH);       cont.add(splitPane,BorderLayout.CENTER);       /*Set bonds for window*/       setBounds(5,5,550,550);       getRootPane().show();       /*Add the listener to the button*/       button.addActionListener(this);       /*Called the function to show the window*/       this.show();    }    /*    Used to write text into textpane    Parameter: writeintotextfield-Object of String class    psendernameattrib-Object of MutableAttributeSet class    Return Value: N/A    */    public void writeintotextfield(String writeintotextfield, MutableAttributeSet psendernameattrib)    {       try       {          scontentmodel.insertString(contentmodel.getLength(), writeintotextfield, psendernameattrib);       }    catch(Exception e)    {    } } /* Method to describe the event Parameter: e-Object of ActionEvent class Return Value: N/A */ public void actionPerformed(ActionEvent e) {    String sendxmlstring="<message to=\""+roomname+"@conference."+servername+"\"    from=\""+user+"@conference.localhost/Home\" type=\"groupchat\">";    sendxmlstring=sendxmlstring+"<body>"+text.getText()+"</body></message>";    sc.sendXMLToJabber(sendxmlstring);    text.setText(""); } } 
end example

Download this listing.

In the above code, the constructor of the GroupChat class takes the object of the SocketConnection class and three strings as input parameters: user, server, and roomname. The user string retrieves the name of an end user. The server string retrieves the name of the Jabber server, and the roomname string retrieves the name of the chat room, which an end user selects. The object of the SocketConnection class helps in invoking the methods of the SocketConnection class.

The methods defined in Listing 4-6 are:

  • clearPane(): Refreshes the text area every time an end user selects a particular group.

  • addElement(): Adds other end users to the selected group.

  • itemintotextfield(): Inserts the text message specified by an end user into the text area provided by the Group Chatting application.

  • actionPerformed(): Acts as an event listener and activates an appropriate method based on the action an end user performs.

The GroupChat.java file creates the Group Chat window that allows end users to participate in a group chat, as shown in Figure 4-8:

this figure shows the group chat window to send and receive messages within the selected group.
Figure 4-8: The Group Chat 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