3

別のスレッドでもう1つのステージを開くのに問題があります。同じスレッドでこのステージを開いている場合、例外は表示されません。

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

メソッド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)
4

1 に答える 1

8

JavaFXのすべてのUI操作は、FXアプリケーションスレッドで実行する必要があります。

コードは次のとおりです。

  Thread t; 
  t = new Thread(actions2methods.get(text));
  t.start(); // Start the thread 

tメソッドを実行するスレッドです。提供されたログに記載されているように、明らかにFXスレッドではありません。java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3

FXスレッドで実行する場合はRunnable、次のコードを使用します。

  Platform.runLater(actions2methods.get(text));
于 2012-08-29T15:52:35.740 に答える