2012-08-29 26 views
3

我在另一個線程中打開另一個舞臺時出現問題。如果我在同一個線程中打開此階段,則不會出現任何例外情況。IllegalStateException在另一個線程中創建舞臺時

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) 
+0

與Swing,我想你需要確保的變化只發生在FX的事件處理線程上。 –

回答

8

JavaFX的所有UI操作應perfromed上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)); 
+1

也許我誤解了你,但是當我擺脫了Tread並寫了Platform.runLater(actions2methods.get(text));相反,我仍然收到與以前一樣的信息。 –

+0

什麼是新異常的堆棧? –

+0

異常是一樣的: 線程「線程-3」中的異常java.lang.IllegalStateException:不在FX應用程序線程上; currentThread =線程3 在com.sun.javafx.tk.Toolkit.checkFxUserThread(未知源) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(未知源) at javafx.stage.Stage。 (Unknown Source) at javafx.stage.Stage。 (來源不明) 在projavafx.starterapp.ui.StarterAppMain.newNetCreation(StarterAppMain.java:421)在projavafx.starterapp.ui.StarterAppMain $ 7.run(StarterAppMain.java:355) \t在java.lang中 \t。 Thread.run(Thread.java:722) –

相關問題