0

I have a question on JavaFX. My program is working with a client-server model. The error occurs when calling a method over the server on the client (rmi-callback). I receive following error code when calling the method newGame from the client program:

Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: Not on FX application thread; currentThread = RMI TCP Connection(1)-127.0.0.1
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
    ...

code of the client program:

public class LoginCLIENT extends Application implements Serializable {

    @Override
    public void start(Stage primaryStage) {
        Registry registry;
        try {
            registry = LocateRegistry.getRegistry(4242);
            server = (FPServerInterface)registry.lookup("ServerInterface");

        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NotBoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        //not important code

    }

    public static void main(String[] args) {
        launch(args);
    }

    public void newGame(SessionInterface Opponent) throws RemoteException{
        alert = new Alert(AlertType.CONFIRMATION, "Yes or No"); 
        Optional<ButtonType>result = alert.showAndWait(); 
        if (result.isPresent() && result.get() == ButtonType.OK){
            System.out.println("Lets play!!!");
        }
    }
fabian
  • 67,623
  • 12
  • 74
  • 102
Ikarus
  • 11
  • 1
  • 4
  • I'm not familiar with rmi, but it seems to use a thread other than the javafx application thread. Use `Platform.runLater` to run code on the application thread. Also I'm not sure whether you should make the `Application` class serializable. All JavaFX classes I know are not serializable and a single field of a non-serializable type in `Application` would ruin the serialisation... Furthermore it's always a good idea to seperate back&front end... – fabian Mar 09 '18 at 23:17
  • Thank you for your answer. I removed the keyword "serializable" – Ikarus Mar 10 '18 at 18:03

1 Answers1

0

This is a possible duplicate of How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error?. I don't have enough reputation to leave this as a comment.

The problem is that you are calling the newGame() method from a thread which is not the FX thread. Simply wrap the call inside Platform.runLater(Runnable).

Example:

... your code
final SessionInterface opponent = ...wherever you get the opponent from
Platform.runLater(() -> newGame(opponent));
E. Makarovas
  • 566
  • 6
  • 6