Hi,
Here is the full code. It is just one class. I have already subcribed to the user who im sending to through another client. If I replace the textArea.append line with System.out.println then the incoming packet is displayed in the console window. But using textArea.append does not show the incoming packet. Please look at the code below.
Thanks
Dilip
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.text.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.filter.*;
public class TextDemo extends JPanel implements ActionListener, PacketListener
{
public static void main (String[] args) throws Exception
{
XMPPConnection connection = new XMPPConnection (“localhost”);
connection.login (“pateldw”, “intel1”);
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
createAndShowGUI ();
}
}
);
}
JTextField textField;
JTextArea textArea;
JEditorPane editorPane;
private final static String newline = “\n”;
public TextDemo ()
{
super (new GridBagLayout ());
textField = new JTextField (20);
textField.addActionListener (this);
textArea = new JTextArea (5, 20);
textArea.setEditable (true);
JScrollPane scrollPane = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints ();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add (textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add (scrollPane, c);
}
public void actionPerformed (ActionEvent evt)
{
try
{
XMPPConnection connection = new XMPPConnection (“localhost”);
connection.login (“pateldw”, “intel1”);
Chat newChat = connection.createChat (“test@localhost”);
String text = textField.getText ();
textArea.append(text + newline);
newChat.sendMessage (text + newline);
textField.selectAll ();
PacketFilter filter = new PacketTypeFilter (Message.class);
TextDemo listener = new TextDemo ();
// listener.start ();
connection.addPacketListener (listener, filter);
}
catch (Throwable t)
{
System.out.println (“Got Error in costructor”);
t.printStackTrace (System.out);
}
//Make sure the new text is visible, even if there
//was a selection in the text area.
textArea.setCaretPosition (textArea.getDocument ().getLength ());
}
public void processPacket (Packet packet)
{
Message message = (Message) packet;
textArea.append(packet.getFrom ());
textArea.append(message.getBody ()):
textField.selectAll ();
}
/**
-
Create the GUI and show it. For thread safety,
-
this method should be invoked from the
-
event-dispatching thread.
*/
private static void createAndShowGUI ()
{
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated (true);
//Create and set up the window.
JFrame frame = new JFrame (“TextDemo”);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new TextDemo ();
newContentPane.setOpaque (true); //content panes must be opaque
frame.setContentPane (newContentPane);
//Display the window.
frame.pack ();
frame.setVisible (true);
}
}