tags:

views:

434

answers:

2

I'm already working on this for weeks to get it working but without success. I'm using Javafx and java for implementing a small chat programm using the smack API (xmpp). The problem is how to get the received messages from java to javafx. The code below is actualy working when I have variable with a static string like "test message" and calling postMessage in java to send it to javafx. But how do I get another object (MessageListener) from java calling postMessage?

public class Java_callback implements RunnableFuture {
FXListener listener;

public String testmsg;


public Java_callback(FXListener listener) {
    this.listener = listener;

}

@Override
public void run() throws Exception {

    postMessage(testmsg);
}

public void postMessage(final String msg) {
    Entry.deferAction(new Runnable() {
        @Override
        public void run() {
            listener.callback(msg);
        }
    });
}
}

Here the code for processing the message. This part somehow need to call postMessage from the code above.

    public void xmpp_create_chat(String msg) {

    ChatManager chatmanager = connection.getChatManager();
    Chat newChat = chatmanager.createChat("admin@unfc", new MessageListener() {

        @Override
        public void processMessage(Chat chat, Message message) {
            //msgArray.add( new String("Received message: " + message.getBody()) );
            //msg_return = "blabla";

            //java_callback.postMessage(msg_return);
            //test.postMessage(message.getBody());
            //System.out.println("Received message: " + message);

        }
    });
A: 

The class containing your second function has to have a reference to the first object, so that it can make the call.

What needs here is so basic that it's hard to figure out what an example might be.

something like this for your second class:

class MessageReceiver {
  private Java_callback callback;

  public void setJava_callback(Java_callback callback) {
    this.callback = callback;
  }
  ....
   void process_message(...) { // inside your inner class
     calllback.postMessage(msg);
   }

}

Is the magic recipe that inner classes can refer to fields of their containing class?

bmargulies
Do you have an example? Sorry, I'm new to java.
Chris-NTA
Dolph Mathews provided me with a solution. Thank you anyway.
Chris-NTA
+3  A: 

If you make postMessage() static, as in:

public static void postMessage(final String msg);

... then you can make a static method call to it from processMessage(), as in:

Java_callback.postMessage(msg_return);

UPDATE: Change your initializion of listener as well.

private static FXListener listener;

Hopefully this doesn't break something outside of the provided code :)

Dolph
public static void postMessage(final String msg) is not working because listener.callback(msg);(error message: "non-static variable listener cannot be refernced from a static context"
Chris-NTA
See my update above. That will fix your error, but I'm paranoid it will break something else.
Dolph
Many Thanks, it's working.
Chris-NTA
No problem! Glad to help.
Dolph