powered by Jive Software

Trouble receiving messages in Smack

I am having trouble receiving messages in chat. I am building a simple SWT application which supports chat. It sends messages in a chat fine, but when the user at the other end sends a message it is not received in my chat window. The chat mechanism is controlled by two java classes. One which deals with creates the chat and deals with the connection etc and another class which deals with the SWT GUI.

Here is the code the class which creates the chat and deals with the connection:

package instantmessengerplugin;
package instantmessengerplugin; import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableItem;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message; public class Chatter {
     
     public XMPPConnection connection;
     public String user;
     public ClassView classView;
     public Chat chat;
     
     
     public Chatter(XMPPConnection connection1,String user1, Display dist)
     {
          connection  = connection1;
          user = user1;
          openChat();
          classView = new ClassView(dist,chat);
          
          
     }
     
     public void  openChat()
     {
          ChatManager cm = connection.getChatManager();
          chat = cm.createChat(user, new MessageListener()
          {
               public void processMessage(Chat chat ,Message message)
               {
                    TableItem item = new TableItem(classView.chatViewer,SWT.NONE);
                    item.setText("Them: " + message);
               }
          }
          
          
          
          
          );
          
          
     }
     
      }

and here is the code for the SWT GUI class:

package instantmessengerplugin; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet; public class ClassView {
     
     public Display displayChat;
     public Shell shellChat;
     final Table chatViewer;
     public Chat chat;
     
     public ClassView(Display dist,Chat chat1){
          
          chat = chat1;
          
          displayChat = dist;
          shellChat = new Shell(displayChat);
          GridLayout gridLayout = new GridLayout();
          gridLayout.numColumns = 2;
          shellChat.setLayout(gridLayout);
          
          Label contact = new Label(shellChat,SWT.NONE);
          GridData data = new GridData();
          data.horizontalAlignment = GridData.FILL;
          data.horizontalSpan = 2;
          data.grabExcessHorizontalSpace = true;
          data.grabExcessVerticalSpace = true;
          
          chatViewer = new Table(shellChat,SWT.NONE);           data = new GridData();
          data.horizontalSpan = 2;
          data.horizontalAlignment = GridData.FILL;
          data.verticalAlignment = GridData.FILL;
          data.grabExcessHorizontalSpace = true;
          chatViewer.setLayoutData(data);
          
          final Text chatBox = new Text(shellChat,SWT.SINGLE);
          data = new GridData();
          data.verticalAlignment = GridData.FILL;
          data.horizontalAlignment = GridData.FILL;
          data.grabExcessHorizontalSpace = true;
          chatBox.setLayoutData(data);
          
          Button send = new Button(shellChat,SWT.PUSH);
          send.setText("Send");
          
          send.addSelectionListener(new SelectionAdapter(){
     
               public void widgetSelected(SelectionEvent e)
               {
                    
                    String s = chatBox.getText();
                    TableItem item = new TableItem(chatViewer, SWT.NONE);
                    item.setText("Me: " + s);
                    try {
                         chat.sendMessage(s);
                    } catch (Exception e1) {
                         // TODO Auto-generated catch block
                         e1.printStackTrace();
                    }
                    
                    chatBox.setText("");
               }
               
          });
     
          
          
          
          
          
          
          
          
          
          shellChat.pack();
          shellChat.open();
          
          while(!shellChat.isDisposed())
          {
               if(!displayChat.readAndDispatch())
               {
                    displayChat.sleep();
               }
          }           
          
          
          
          
          
          
     }
     
     
     public void updateChat(Message message, Table table)
     {
          TableItem item = new TableItem(table,SWT.NONE);
          item.setText(message +"");
     }
     
     
     
}

There’s a few imports in there that aren’t being used, I haven’t cleaned it up just yet.

As you can see in the Chatter class I have implemented the processMessage method which I believe should display the message but it doesn’t.

Is there something I am missing or am I going about this the completely wrong way?