在另一个线程中创建一个阶段时出现IllegalStateException

Vla*_*kov 3 java user-interface javafx-2

我在另一个线程中再打开一个阶段时遇到问题.如果我在同一个线程中打开此阶段,则不会出现异常.

void hashMapDeclaration(){
    actions2methods.put("NEW", new Runnable() {@Override public void run() { newNetCreation(); }});
    actions2methods.put("LOAD", new Runnable() {@Override public void run() { loadNetState(); }});
    ......  //other hashes
}

HBox buttonBuilder(double spacing,double layoutX,String... bNames){
    HBox lBar = new HBox(10);
    .... //some code
    for(final String text : bNames){ //in my case text variable value is "NEW" so it should run method newNetCreation
        Button newButton = new Button();
        newButton.setText(text);
        .... //code
        newButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent paramT) {
                Thread t;
                EventQueue.isDispatchThread();
                t = new Thread(actions2methods.get(text));
                t.start(); // Start the thread 
                System.out.println("button pressed");
            }
        });
        lBar.getChildren().add(newButton);
    }
    return lBar;
}

void newNetCreation(){
    final Stage dialogStage = new Stage();
    final TextField textField;
    dialogStage.initOwner(stage);
    dialogStage.initModality(Modality.WINDOW_MODAL);   
    dialogStage.setFullScreen(false);
    dialogStage.setResizable(false);
    dialogStage.setScene(SceneBuilder
        .create()
        .fill(Color.web("#dddddd"))
        .root(textField = TextFieldBuilder
                            .create()
                            .promptText("Enter user name")
                            .prefColumnCount(16)
                            .build()
        )
        .build()
    );
    textField.textProperty().addListener(new ChangeListener() {
        public void changed(ObservableValue ov, Object oldValue, Object newValue) {
            System.out.println("TextField text is: " + textField.getText());
        }
    });
    dialogStage.show();
    System.out.println("new net");
}
Run Code Online (Sandbox Code Playgroud)

方法newNetCreation是导致问题的方法.我程序中的所有操作都存储在HashMap中.方法buttonBuilder创建新线程,并应根据变量值启动方法,在我的情况下,他必须调用newNetCreation方法,但是当他尝试时,会发生以下异常:

Exception in thread "Thread-3" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source)
at javafx.stage.Stage.<init>(Unknown Source)
at javafx.stage.Stage.<init>(Unknown Source)
at projavafx.starterapp.ui.StarterAppMain.newNetCreation(StarterAppMain.java:400)
at projavafx.starterapp.ui.StarterAppMain$7.run(StarterAppMain.java:354)
at java.lang.Thread.run(Thread.java:722)
Run Code Online (Sandbox Code Playgroud)

Ser*_*nev 8

JavaFX的所有UI操作都应在FX应用程序线程上执行.

这是你的代码:

  Thread t; 
  t = new Thread(actions2methods.get(text));
  t.start(); // Start the thread 
Run Code Online (Sandbox Code Playgroud)

t是您运行方法的线程.它显然不是你所提供的日志中所述的FX线程:java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3

如果要Runnable在FX线程上运行,请使用下一个代码:

  Platform.runLater(actions2methods.get(text));
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢!runLater帮助.编辑错误的方法是我的错.一些睡眠永远不会伤害,思想....... (2认同)