Initiating a Private Chat


The ChatWindow.java file creates the user interface that allows end users to initiate a private chat. Listing 4-5 shows the contents of the ChatWindow.java file:

Listing 4-5: The ChatWindow.java File

start example
 /*Import required swing classes*/ import javax.swing.*; /*Import required swing event classes*/ import javax.swing.event.*; /*Import required swing text classes*/ import javax.swing.text.*; /*Import required awt classes*/ import java.awt.*; import java.awt.event.*; public class  ChatWindow extends JFrame implements ActionListener {    /*Declare object of Container class.*/    Container container;    GridBagLayout gridbaglayout;    /*Declare object of JLabel class.*/    JLabel fromuser = null;    JLabel touser = null;    JLabel fromusername = null;    JLabel tousername = null;    /*Declare object of JTextPane class.*/    JTextPane textarea = null;    JTextField textfield = null;    /*Declare object of JButton class.*/    JButton sendbutton = null;    /*Declare object of JScrollPane class.*/    JScrollPane scpane;    /*Declare string variable.*/    String user="";    String receiver="";    String servername="";    String messagestring="";    /*Declare object of SocketConnection class.*/    SocketConnection clientsocket;    Document contentmodel;    MutableAttributeSet recervernameatrib;    ChatWindow chat;    /*    class ChatWindow - This class is used to create a GUI for chatting.    Constructor:    ChatWindow-This constructor creates GUI.    Methods:    setObj - Used to set the use object of ChatWindow    insertTextInToTextPane - Used to write the text into the textpane    */    /* Constructor for class*/    public ChatWindow(String user, String receiver, String servername, SocketConnection clientsocket)    {       this.user=user;       this.receiver=receiver;       this.servername=servername;       this.clientsocket=clientsocket;       fromuser = new JLabel("From :");       fromusername  = new JLabel(user);       String touser1= receiver;       touser = new JLabel("To :");       tousername = new JLabel(touser1);       /*Set the window title*/       setTitle("Group Chatting Application: "+touser1);       container = this.getContentPane();       recervernameatrib=new SimpleAttributeSet();       StyleConstants.setFontFamily(recervernameatrib,"Verdana");       StyleConstants.setForeground(recervernameatrib,Color.red);       gridbaglayout=new GridBagLayout();       GridBagConstraints gridbagconstraints=new GridBagConstraints();       /*Creating a panel with gridbaglayout*/       JPanel jpanel1 = new JPanel(gridbaglayout);       gridbagconstraints.fill=GridBagConstraints.BOTH;       gridbagconstraints.insets=new Insets(5, 5, 0, 0);       gridbagconstraints.gridx=0;       gridbagconstraints.gridy=0;       gridbagconstraints.weightx=1;       gridbagconstraints.weighty=1;       gridbagconstraints.anchor=GridBagConstraints.WEST;       gridbaglayout.setConstraints(fromuser, gridbagconstraints);       fromuser.setPreferredSize(new Dimension(140, 28));       /*Adding component to panel*/       jpanel1.add(fromuser);       gridbagconstraints.fill=GridBagConstraints.BOTH;       gridbagconstraints.insets=new Insets(5, 5, 0, 20);       gridbagconstraints.gridx=1;       gridbagconstraints.gridy=0;       gridbagconstraints.weightx=1;       gridbagconstraints.weighty=1;       gridbagconstraints.anchor=GridBagConstraints.EAST;       gridbaglayout.setConstraints(fromusername,gridbagconstraints);       fromusername.setPreferredSize(new Dimension(20, 28));       fromusername.setFont(new Font("Verdana", Font.BOLD, 10));       /*Adding the component to panel*/       jpanel1.add(fromusername);       gridbagconstraints.fill=GridBagConstraints.BOTH;       gridbagconstraints.insets=new Insets(5, 5, 0, 0);       gridbagconstraints.gridx=2;       gridbagconstraints.gridy=0;       gridbagconstraints.weightx=1;       gridbagconstraints.weighty=1;       gridbagconstraints.anchor=GridBagConstraints.EAST;       gridbaglayout.setConstraints(touser,gridbagconstraints);       touser.setPreferredSize(new Dimension(140, 28));       /*Adding the component to panel*/       jpanel1.add(touser);       gridbagconstraints.fill=GridBagConstraints.BOTH;       gridbagconstraints.insets=new Insets(5, 5, 0, 0);       gridbagconstraints.gridx=3;       gridbagconstraints.gridy=0;       gridbagconstraints.weightx=1;       gridbagconstraints.weighty=1;       gridbagconstraints.anchor=GridBagConstraints.WEST;       gridbaglayout.setConstraints(tousername, gridbagconstraints);       tousername.setPreferredSize(new Dimension(20,28));       tousername.setFont(new Font("Verdana", Font.BOLD, 10));       /*Adding component to panel*/       jpanel1.add(tousername);       /*Adding panel to container*/       container.add(jpanel1,BorderLayout.NORTH);       /*Creating a new panel*/       JPanel jpanel2 = new JPanel();       /*Creating new textpane*/       textarea = new JTextPane();       contentmodel=textarea.getDocument();       /*Set the size of textpane*/       textarea.setPreferredSize(new Dimension(80, 20));       /*Creating a scrollpane and adding a textpane in to the scrollpane*/       scpane  = new JScrollPane(textarea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.       HORIZONTAL_SCROLLBAR_AS_NEEDED);       container.add(scpane,BorderLayout.CENTER);       /*Creating a panel with gridbaglayout*/       JPanel jpanel3 = new JPanel(gridbaglayout);       textfield = new JTextField();       /*Creating a object of send button*/       sendbutton = new JButton("Send");       gridbagconstraints.fill=GridBagConstraints.BOTH;       gridbagconstraints.insets=new Insets(5, 5, 0, 0);       gridbagconstraints.gridx=0;       gridbagconstraints.gridy=0;       gridbagconstraints.weightx=1;       gridbagconstraints.weighty=1;       gridbagconstraints.anchor=GridBagConstraints.NORTH;       gridbaglayout.setConstraints(textfield, gridbagconstraints);       textfield.setPreferredSize(new Dimension(120, 28));       /*Adding a textfield into panel*/       jpanel3.add(textfield);       gridbagconstraints.fill=GridBagConstraints.BOTH;       gridbagconstraints.insets=new Insets(5, 5, 0, 0);       gridbagconstraints.gridx=1;       gridbagconstraints.gridy=0;       gridbagconstraints.weightx=0;       gridbagconstraints.weighty=0;       gridbagconstraints.anchor=GridBagConstraints.EAST;       gridbaglayout.setConstraints(sendbutton, gridbagconstraints);       sendbutton.setPreferredSize(new Dimension(60, 28));       /*Adding a send button into panel*/       jpanel3.add(sendbutton);       /*Adding action listener in to sendbutton*/       sendbutton.addActionListener(this);       /*Adding action listener in to textfield*/       textfield.addActionListener(this);       /*Adding a panel to container*/       container.add(jpanel3,BorderLayout.SOUTH);       /*Set the bounds for window*/       setBounds(100,100,350,280);       /*Called the show method to display the window*/       show();    }    /*Method to describe the event*/    public void actionPerformed(ActionEvent e)    {       if(e.getSource()==sendbutton)       {          if (!textfield.getText().trim().equals(""))          {          String[] usersplited=user.split("@");          messagestring="<message type='chat' from='"+usersplited[0]+"@localhost/Home'          to='"+receiver+"@"+servername+"/Home' >";          messagestring=messagestring+"<body>";          messagestring=messagestring+textfield.getText().trim();          messagestring=messagestring+"</body></message>";          try          {             contentmodel.insertString(contentmodel.getLength(), "\n"+user+":             "+textfield.getText(),recervernameatrib);          }          catch(Exception ble)          {          }          clientsocket.sendXMLToJabber(messagestring);          textfield.setText("");       }    }    if(e.getSource()==textfield)    {    if (!textfield.getText().trim().equals(""))    {       String[] usersplited=user.split("@");       messagestring="<message type='chat' from='"+usersplited[0]+"@localhost/Home'       to='"+receiver+"@"+servername+"/Home' >";       messagestring=messagestring+"<body>";       messagestring=messagestring+textfield.getText().trim();       messagestring=messagestring+"</body></message>";       try       {          contentmodel.insertString(contentmodel.getLength(), "\n"+user+":          "+textfield.getText(), recervernameatrib);       }       catch(Exception ble)       {       }       clientsocket.sendXMLToJabber(messagestring);       textfield.setText("");       }    } } /* Function to set the use object of ChatWindow Parameter: chat - Object of ChatWindow class. Return Value: N/A */ public void setObj(ChatWindow chat) {    this.chat=chat; } /* Used to write the text into the textpane Parameter: message1 - Object of String class. sendername1-Object of String class. Return Value: N/A */ public void insertTextInToTextPane(String message1, String sendername1) {    try    {       contentmodel.insertString(contentmodel.getLength(), "\n"+sendername1+":       "+message1,recervernameatrib);    }    catch(BadLocationException ble)    {    } } } 
end example

Download this listing.

In the above code, the constructor of the ChatWindow class takes the object of the SocketConnection class and three strings as input parameters: user, server, and receiver. The user string retrieves the name of an end user. The server string retrieves the name of the Jabber server and the receiver string retrieves the name of the end user at the receiving end. The object of the SocketConnection class helps in invoking the methods of the SocketConnection class.

The methods defined in Listing 4-5 are:

  • setObj(): Sets the object of the ChatWindow class.

  • insertTextInToTextPane(): 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 ChatWindow.java file creates a Private Chat window that allows the end user to initiate a private chat, as shown in Figure 4-7:

this figure shows the private chat window to send and receive private messages.
Figure 4-7: Private 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