2012-08-29 3 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

, 당신이 반드시 변경 사항은 발생해야 가정 : java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3

당신이 FX 스레드에서 Runnable를 실행하려면이, 다음 코드를 사용 FX의 이벤트 처리 스레드에서. –

답변

8

자바 FX에 대한 모든 UI 작업이에 perfromed한다 FX 응용 프로그램 스레드

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

t 당신이 당신의 메소드를 실행 스레드 :

여기에 코드입니다. 그것은 분명히 기록에 명시된 바와 같이 FX 스레드 아니다 당신이 제공 :

스윙처럼
Platform.runLater(actions2methods.get(text)); 
+1

아마도 내가 너를 오해하고있다. 그러나 Tread를 없애고 Platform.runLater (actions2methods.get (text))를 썼을 때; 대신 이전과 동일한 메시지가 나타납니다. –

+0

새로운 예외의 스택 추적은 무엇입니까? –

+0

예외는 동일합니다. "Thread-3"스레드의 예외 java.lang.IllegalStateException : FX 응용 프로그램 스레드가 아닙니다. currentThread = Thread-3 com.sun.javafx.tk.Toolkit.checkFxUserThread (알 수없는 소스) com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread (알 수없는 소스) at javafx.stage.Stage. (알 수없는 출처) at javafx.stage.Stage. \t있는 java.lang에서 projavafx.starterapp.ui.StarterAppMain $ 7.run (StarterAppMain.java:355) \t에서 projavafx.starterapp.ui.StarterAppMain.newNetCreation (StarterAppMain.java:421)에서 (알 수없는 소스) . Thread.run (Thread.java:722) –

관련 문제