JavaFX应用程序线程

时间:2018-03-09 22:40:37

标签: java javafx rmi

我对JavaFX有疑问。我的程序正在使用客户端 - 服务器模型。在客户端上通过服务器调用方法时发生错误(rmi-callback)。从客户端程序调用方法newGame时收到以下错误代码:

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)
    ...

客户端程序的代码:

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!!!");
        }
    }

1 个答案:

答案 0 :(得分:0)

这可能是How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error?的副本。我没有足够的声誉将此作为评论。

问题是你从一个不是FX线程的线程调用newGame()方法。只需将呼叫包裹在... your code final SessionInterface opponent = ...wherever you get the opponent from Platform.runLater(() -> newGame(opponent)); 内。

示例:

{{1}}