How to put messages in a gui

Hello. I am trying to write a simple jabber client. To start with I like to write incoming messages to a textfield in a simple swing gui.

I am able to write the incoming messages to console, but I am having problems with trigging an action that will write the messages to the textfield in a gui…

Do you have a simple example of how to make an action of incoming messages and write them to a window?

I am quite new to this. But I hope that you will be friendly and give me a hint.

regards

R Martin

Here is a simple client implementation:

// Establish the connection

XMPP connection = new XMPPConnection(“yourServer”);

// This must be turned off if you are not setting the thread id’'s. Otherwise, your

// messages wont be routed at all.

Chat.setFilteredOnThreadID(false);

// Setup packet listener to start new buddy conversations on new chat invitation.

PacketListener pl = new PacketListener() {

public void processPacket(Packet packet) {

if (packet instanceof Message) {

Message message = (Message)packet;

String participant = message.getFrom();

// If no chat session currently exists with this participant, start one.

if ( ) {

Chat chat = new Chat(connection, participant);

/* Do something with newly created chat */

}

// Else, send the existing buddy conversation the message.

else {

/* Route message to existing chat which will add the message

something like this (assume that it uses a JEditorPane):

int offset = yourJEditorPane.getDocument().getLength();

yourJEditorPane.getDocument().insertString(offset,message,

null);

*/

}

};

connection.addPacketListener(pl, null);[/code]

Although the above is only a fragment of what you will actually need, it contains the two main things you were questioning: how to react to incoming packets, and how to add text to a swing component.

-Ken

Hi R Martin,

If you’‘re still having troubles after following Ken’‘s advice, you might want to try posting a small portion of your existing program or sample program that we can look at to see where you’'re running into trouble.

Thanks,

Ryan

Hey. Thanks for your help.

I am pretty sure this is very simple, but I still havent managed to put the result in a gui.

Ill try to put the parts of code that may be interesting here:

When a incoming message is coming, it calls the gui like this:

new gui().incoming(message.getBody());


“incoming” is like this:

public void incoming(String input) {

//should start some kind of actionevent??

jTextField.setText(input);

}


jTextField if like this (rendered with eclipse VE):

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField();

jTextField.setText(“Write something”);

jTextField.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

System.out.println(“actionPerformed() JTextField”); // TODO Auto-generated Event stub actionPerformed()

}

});

}

return jTextField;

}

Thanks again for taking your time to help me.

Regards

.Martin

Hi Martin,

Nothing is jumping out at me as to why your textfield is not being updated. I have not worked much with Eclipse’'s GUI builder, is it possible that your textfield has not been instanciated before you call the setText() method? Below is a simple program that updates a textarea that you might want to take a look at for ideas:

import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.PacketListener;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.filter.PacketFilter;

import org.jivesoftware.smack.filter.PacketTypeFilter;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.packet.Packet;

import javax.swing.JDialog;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class ParrotBotUI extends JDialog implements PacketListener {

private JScrollPane scrollPane = null;

private JTextArea textAreaConverstation = new JTextArea();

private XMPPConnection connection = null;

private Chat chat = null;

private PacketFilter filter = null;

private static String server = “localhost”;

private static String user = “test”;

private static String password = “test”;

public static void main(String[] args) {

new ParrotBotUI().squawk();

}

public ParrotBotUI() {

scrollPane = new JScrollPane(textAreaConverstation);

getContentPane().add(scrollPane);

setTitle(“ParrotBotUI”);

setSize(300, 200);

setVisible(true);

try {

//XMPPConnection.DEBUG_ENABLED = true;

connection = new XMPPConnection(server);

connection.login(user, password);

textAreaConverstation.append(“Connected”);

textAreaConverstation.append("\n");

filter = new PacketTypeFilter(Message.class);

connection.addPacketListener(this, filter);

} catch (XMPPException e) {

e.printStackTrace();

}

}

private void squawk() {

while (true) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public void processPacket(Packet packet) {

Message message = (Message) packet;

chat = connection.createChat(packet.getFrom());

try {

String body = message.getBody();

chat.sendMessage(body);

textAreaConverstation.append("<" + message.getFrom() + "> ");

textAreaConverstation.append(body);

textAreaConverstation.append("\n");

} catch (XMPPException e) {

e.printStackTrace();

}

}

}

/code

Hope that helps,

Ryan

Martin,

Try putting your setText() in a runnable like this:

Runnable runnable = new Runnable() {

public void run() {

yourTextField.setText(newText);

}

};

SwingUtilities.invokeLater(runnable);

I’'m not sure if that was your problem, but it certainly could be. Your not really supposed to make modificaitons to GUI elements anywhere but the event dispatch thread (which is where the SwingUtilities.invokeLater() function runs your runnable.

Give Ryans’‘s code a try though. He’'s provided a great example which should do exactly what you want.

-Ken

Message was edited by:

kenneth.orr

Hey guys. Thanks a lot for your friendly replies. You have helped me a lot, and I am making progress again. -and it is fun to work :).

Regards.

R Martin

Hi Matin,

Glad to hear things are working for you. Please post again if you need any more assistance.

Thanks,

Ryan