Sending Messages


The MessageClass.java file creates the user interface that allows the end user to specify the user ID and message. The MessageClass class sends the message to the user ID specified by the end user. Listing 2-5 shows the contents of the MessageClass.java file:

Listing 2-5: The MessageClass.java File

start example
 /*Imports required swing classes.*/ import javax.swing.*; import javax.swing.event.*; /*Imports required awt classes.*/ import java.awt.*; import java.awt.event.*; /* class MessageClass-This class is used to create a GUI for sending messages. Constructor: MessageClass-This constructor creates GUI. Methods: actionPerformed-This method is called when end user clicks on send button. */ class MessageClass extends JFrame implements ActionListener {    /*Declares objects of JLabel class.*/    JLabel useridlabel;    JLabel messagelabel;    JLabel lastmessagelabel;    JLabel firstmessagelabel;    /*Declares object of JTextField class.*/    JTextField useridtext;    /*Declares object of JTextArea class.*/    JTextArea messagetextpane;    /* Declares object of JScrollPane class.*/    JScrollPane messagescrollpane;    /*Declares objects of JButton class.*/    JButton closebutton;    JButton sendbutton;    /* Declares objects of JPanel class.*/    JPanel usermessagepanel;    JPanel messagepanel;    JPanel buttonpanel;    /*Declares objects of JabberClient class.*/    JabberClient jabberclient;    public MessageClass(JabberClient jabberclient)    {       super("Message Window");       Container contentpane=getContentPane();       this.jabberclient=jabberclient;       contentpane.add(new JLabel("  "),BorderLayout.WEST);       /*        Initializes the object of JLabel class        */       useridlabel=new JLabel("User ID");       /*        Initializes the object of JTextField class        */       useridtext=new JTextField();       /*        Initializes the object of JLabel class        */       messagelabel=new JLabel("Message");       /*       Initializes the object of JTextArea class        */       messagetextpane=new JTextArea();       /*        Initializes the object of JScrollPane class        */       messagescrollpane=new JScrollPane(messagetextpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,       JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);       /*       Initializes the object of JPanel class        */       usermessagepanel=new JPanel();       /*       Initializes the objects of JButton class.       */       closebutton=new JButton("Close");       sendbutton=new JButton("Send Message");       /*       Adds the ActionListener to the object of the JButton class.        sendbutton.addActionListener(this);       closebutton.addActionListener(this);       /*        Sets the ActionCommand of the object of JButton class.       */       sendbutton.setActionCommand("send");       closebutton.setActionCommand("close");       /*        Declare and initialize the object of GridLayout class.        */       GridLayout usermessagegridlayout=new GridLayout(2, 2, 5, 5);       /*        Set the Gridlayout to usermessagepanel object.        */       usermessagepanel.setLayout(usermessagegridlayout);       /*        Initializes the objects of JLabel class.       */       firstmessagelabel=new JLabel("Message", JLabel.RIGHT);       usermessagepanel.add(firstmessagelabel);       /*       Initializes the objects of JLabel class.       */       lastmessagelabel=newJLabel("Window", JLabel.LEFT);       firstmessagelabel.setFont(newFont("Verdana", Font.BOLD, 12));       lastmessagelabel.setFont(new Font("Verdana", Font.BOLD, 12));       usermessagepanel.add(lastmessagelabel);       /*        Adds useridlabel to usermessagepanel       */       usermessagepanel.add(useridlabel);       /*Adds useridtext to usermessagepanel*/       usermessagepanel.add(useridtext);       /*        Initializes the objects of JPanel class.       */       messagepanel=new JPanel();       /*       Declare and initialize the object of GridLayout class.        */       GridLayout messagegridlayout=new GridLayout(1, 2, 5, 5);       /*       Set the Gridlayout to messagepanel object.       */       messagepanel.setLayout(messagegridlayout);       /*Adds messagelabel to messagepanel*/       messagepanel.add(messagelabel);       /*       Adds messagescrollpane to messagepanel       */       messagepanel.add(messagescrollpane);       /*        Declare and initialize the object of JPanel  class.        */       JPanel toppanel=new JPanel(new  GridLayout(2,1,5,5));       /*Adds usermessagepanel to toppanel*/       toppanel.add(usermessagepanel);       /* Adds messagepanel to toppanel*/       toppanel.add(messagepanel);       /*Adds toppanel to contentpane*/contentpane.add(toppanel,BorderLayout.CENTER);       buttonpanel=new JPanel(new GridLayout(1, 2, 0, 0));       buttonpanel.add(new JLabel(" "));       /*       Declare and initialize the object of JPanel class.       */       JPanel subbuttonpanel=new JPanel();       /* Adds buttons to subbuttonpanel*/       subbuttonpanel.add(sendbutton);       subbuttonpanel.add(closebutton);       buttonpanel.add(subbuttonpanel);       contentpane.add(buttonpanel,BorderLayout.SOUTH);       useridtext.setPreferredSize(new  Dimension(80,20));       setSize(400,200);       setVisible(true);    }    public void actionPerformed(ActionEvent ae)    {       String actioncommand=ae.getActionCommand();       String userid=useridtext.getText().trim();       String message=messagetextpane.getText().trim();       String errorstring="";       if (actioncommand.equals("send"))       {          if (userid.equals(""))          {             errorstring="User ID is a required field.\n";          }          if (message.equals(""))          {             errorstring=errorstring+"Message field cannot be left blank.";          }          if (!errorstring.equals(""))          {             JOptionPane.showMessageDialog(null,errorstring,"Error",JOptionPane.PLAIN_MESSAGE);             return;          }          String username=jabberclient.getUserName();          String servername=jabberclient.getServerName();          String sendmessagestring="";          sendmessagestring="<message type='chat' to='"+userid+"/Home'          from='"+username+"@"+servername+"/Home'>\n";          sendmessagestring=sendmessagestring+"<body>\n"+message+"\n";           sendmessagestring=sendmessagestring+"</body>\n</message>";          jabberclient.setMessageInToTextBox(sendmessagestring);       }       setVisible(false);    } } 
end example

Download this listing.

In the above code, the constructor of the MessageClass class takes an object of the JabberClient class as an input parameter. This allows an end user to invoke the methods of the JabberClient class.

The methods defined in Listing 2-5 are:

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

  • getText(): Retrieves the text message specified by the end user.

Select File-> Send Message to send the message to the specified end user. The Message Window appears, as shown in Figure 2-8:

click to expand: this figure shows the user interface of the message window to send messages to any specific end user.
Figure 2-8: The Message Window

The MessageClass.java file allows end users to specify the User ID and Message in the Message 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