Creating the Chat Window


The ChatWindow.java file creates a Chat window to send messages to a particular end user selected in the contact list. Listing 7-5 shows the contents of the ChatWindow.java file:

Listing 7-5: The ChatWindow.java File

start example
 /*Imports required javax.swing classes*/ import javax.swing.*; /*Imports required java.awt classes*/ import java.awt.*; /*Imports required javax.swing.event classes*/ import javax.swing.event.*; /*Imports required java.awt.event classes*/ import java.awt.event.*; /*Imports required javax.swing.text package classes*/ import javax.swing.text.*; /* class ChatWindow - This class is used to create GUI for chatting. Constructor: ChatWindow-This constructor creates GUI. Methods: writemessage: This method is called to send input message to insertTextInToTextPane function. eraseText: This method is called to send input message to insertTextInToTextPane function. addToRosterTree: This method is called to add user name in to tree. */ public class  ChatWindow extends JFrame implements ActionListener { /*Declares object of Container class.*/ Container container; /*Declares object of GridBagLayout class.*/ GridBagLayout gridbaglayout; /*Declares object of JLabel class.*/ JLabel fromuser = null; JLabel touser = null; JLabel fromusername = null; JLabel tousername = null; /*Declares object of JTextPane class.*/ JTextPane receivemessagetextarea = null; /*Declares object of JTextField class.*/ JTextField sendmessagetext = null; /*Declares object of JButton class.*/ JButton sendbutton = null; /*Declares object of JButton class.*/ JScrollPane scpane; /*Declares object of String class.*/ String username; String usernickname; String sendername; /*Declares object of Document class.*/ Document contentmodel; /*Declares object of MutableAttributeSet class.*/ MutableAttributeSet recervernameatrib, sendermessagattrib; /*Declares object of SocketClass class.*/ SocketClass socketchatclass; public ChatWindow(String nickname,String recevername, SocketClass socketclass) {    sendername=socketclass.getUserName();    /*Initializes the objects of JLabel class.*/    username=recevername;    usernickname=nickname;    socketchatclass=socketclass;    fromuser = new JLabel("From :");    touser = new JLabel("To :");    fromusername=new JLabel(socketclass.getUserName());    tousername = new JLabel(usernickname);    /*Sets window title.*/    setTitle("Chat Window: "+usernickname);    container = this.getContentPane();    /*Initializes the objects of GridBagLayout class.*/    gridbaglayout=new GridBagLayout();    /*Declare and initializes the objects of GridBagConstraints class.*/    GridBagConstraints gridbagconstraints=new GridBagConstraints();    /*Declare and initializes the objects of JPanel class.*/    JPanel labelpanel = new JPanel(gridbaglayout);    /*Sets constraints for gridbagconstraints.*/    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);    /*Sets fromuser size.*/    fromuser.setPreferredSize(new Dimension(140, 28));    /*Adds label to labelpanel.*/    labelpanel.add(fromuser);    /*Sets constraints for gridbagconstraints.*/    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);    /*Sets fromusername size.*/    fromusername.setPreferredSize(new Dimension(20, 28));    /*Sets fromusername font.*/    fromusername.setFont(new Font("Verdana", Font.BOLD, 10));    /*Adds fromusername to labelpanel.*/    labelpanel.add(fromusername);    /*Sets constraints for gridbagconstraints.*/    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);    /*Sets touser size.*/    touser.setPreferredSize(new Dimension(140, 28));    /*Adds touser to labelpanel.*/    labelpanel.add(touser);    /*Sets constraints for gridbagconstraints.*/    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);    /*Sets tousername size.*/    tousername.setPreferredSize(new Dimension(20, 28));    /*Sets tousername font.*/    tousername.setFont(new Font("Verdana", Font.BOLD, 10));    /*Adds tousername to labelpanel.*/    labelpanel.add(tousername);    /*Adds labelpanel to container.*/    container.add(labelpanel, BorderLayout.NORTH);    /*Initializes the objects of JTextPane class.*/    receivemessagetextarea = new JTextPane();    contentmodel=receivemessagetextarea.getDocument();    /*Initializes the objects of SimpleAttributeSet class.*/    recervernameatrib=new SimpleAttributeSet();    StyleConstants.setFontFamily(recervernameatrib,"Verdana");    StyleConstants.setForeground(recervernameatrib, Color.black);    sendermessagattrib=new SimpleAttributeSet();    StyleConstants.setFontFamily(sendermessagattrib,"Verdana");    StyleConstants.setForeground(sendermessagattrib,Color.red);    receivemessagetextarea.setPreferredSize(new Dimension(80, 20));    receivemessagetextarea.setEditable(false);    /*Initializes the objects of JScrollPane class.*/    scpane  = new JScrollPane(receivemessagetextarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    /*Adds scpane to container.*/    container.add(scpane,BorderLayout.CENTER);    /*Declare and initializes the objects of JPanel class.*/    JPanel sendmessagepanel = new JPanel(gridbaglayout);    /*Initializes the objects of JTextField class.*/    sendmessagetext = new JTextField();    /*Initializes the objects of JButton class.*/    sendbutton = new JButton("Send");    /*Adds action listener to sendbutton.*/    sendbutton.addActionListener(this);    /*Sets action command of sendbutton.*/    sendbutton.setActionCommand("send");    /*Sets constraints for gridbagconstraints.*/    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(sendmessagetext, gridbagconstraints);    /*Sets tousername size.*/    sendmessagetext.setPreferredSize(new Dimension(120, 28));    /*Adds sendmessagetext to the sendmessagepanel*/    sendmessagepanel.add(sendmessagetext);    /*Sets constraints for gridbagconstraints.*/    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);    /*Sets sendbutton size.*/    sendbutton.setPreferredSize(new Dimension(60, 28));    sendmessagepanel.add(sendbutton);    container.add(sendmessagepanel, BorderLayout.SOUTH);    setBounds(100, 100, 350, 280);    show(); } public void actionPerformed(ActionEvent ae) {    String actioncommand=ae.getActionCommand();    String messagestring;    if (actioncommand.equals("send"))    {       if (!sendmessagetext.getText().trim().equals(""))       {          String recevername=username.substring(1,username.length()-1);          messagestring="<message type='chat' to='"+recevername+"/Home'          from='"+sendername+"@"+socketchatclass.getServerName()+"/Home'>";          messagestring=messagestring+"<body>";          messagestring=messagestring+sendmessagetext.getText().trim();          messagestring=messagestring+"</body></message>";          try          {             contentmodel.insertString(contentmodel.getLength(),"\n"+sendername+":             ",sendermessagattrib);             contentmodel.insertString(contentmodel.getLength(), sendmessagetext.getText().trim(),             recervernameatrib);          }          catch(BadLocationException ble)          {          }          socketchatclass.sendXMLToJabber(messagestring);          sendmessagetext.setText("");          }       }    }    /* insertTextInToTextPane: This method is called to insert text message into textpane. Parameter: pmessage - Object of  String class, psendername - Object of  String class. Return Value: N/A */ public void insertTextInToTextPane(String pmessage,String psendername) {    try    {    contentmodel.insertString(contentmodel.getLength(), "\n"+usernickname+":    "+pmessage,recervernameatrib);    }    catch(BadLocationException ble) {}    } } 
end example

Download this listing.

In the above code, the constructor of the ChatWindow class takes an object of the SocketClass class and two strings, userName and receivername as input parameters. The object of the SocketClass class allows end users to invoke the methods of the SocketClass class. The userName string retrieves the user name of the selected end user, and the receivername string retrieves the user ID of the selected end user.

The methods defined in Listing 7-5 are:

  • insertTextInToTextPane(): Inserts the text messages specified by end users in the text area of the Chat window.

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

The ChatWindow.java file creates the Chat window to send messages to the selected end user, as shown in Figure 7-7:

this figure shows the chat window of the contact list application.
Figure 7-7: The 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