A couple of months ago, I wrote a JavaFX chat client using JavaFX 1.1 --- see my previous blog entry. The article below describes the changes that are needed to port this chat client to JavaFX 1.2.
The main difficulty in the JavaFX 1.1 chat client was to get the asynchronous stuff working: we have an open connection towards the chatserver, and new chatentries from all clients are pushed through this connection to the JavaFX client. Clearly, we cannot block the Event Dispatching Thread for achieving this. Starting a new Thread in JavaFX and realizing a non-blocking communication between this thread and the EDT was not trivial in JavaFX 1.1, but the chat client that was discussed in my old blog entry did the job.
The changes between JavaFX 1.1 and JavaFX 1.2 are described here and indeed, the asynchronous functionality has changed considerably.
Some general information about the JavaFX 1.2 Async model can be found in this blog entry from Baechul. In short, the most important changes are:
The following diagram is therefore still valid in JavaFX 1.2:
AsyncReader class extended com.sun.javafx.runtime.async.AbstractAsyncOperation in the 1. 1 version. That concept is being replaced by the javafx.async.RunnableFuture interface. Hence, we have public class AsyncReader implements RunnableFutureThe
RunnableFuture interface has a public void run() method that we should implement. In the old AbstractAsyncOperation class, the run-logic was in the call method. Instead of public Object call() throws Exceptionwe now have
public void run() throws Exception
com.sun.javafx.runtime.async.AsyncOperationListener class anymore, so the constructor for the AsyncReader simply becomes public AsyncReader(ChatInput callback) { this.callback = callback;
}Note that the callback parameter is a Java interface that is implemented by the
ChatHistoryBox.fx class. This allows the java class AsyncReader to call into the JavaFX class ChatHistoryBox. javafx.async.AbstractAsyncOperation is more or less replaced by JavaTaskBase, the ChatReader.fx class now extends JavaTaskBase JavaTaskBase requires a create-method. This method is called by the start() method, and it should return a RunnableFuture instance. This is more or less what we did before in the start() method of the AbstractAsyncOperation, hence we transformed the behavior of old start() method into the new create() method. The create() method creates an AsyncReader instance (a Java object implementing RunnableFuture) and returns this. AsyncWriter and ChatWriter AsyncReader, we used the invokeLater() Swing method for notifying the EDT of changes that should be made. It is recommended to use the JavaFX.deferAction for this, though: javafx.lang.FX.deferAction(new Function0() { public Void invoke() { callback.gotText(oneline);
return null;
}
});
ChatReader by calling the start method. This is done in Main.fx, after the ChatReader has been created. ChatWriter instance needs to be created and started. com.sun specific classes have been replaced by javafx classes, which is good of course. The communication from the Java class back into the javaFX EDT, using FX.deferAction can probably be simplified in future releases. written on 15:32.
Create commentDecember October March January
November September August May February January
December November October September July June May March February January
December November October September August July June May April March February
December November October September August July June May April March February January