2016-10-31 3 views
0

자신의 컨트롤러를 사용하는 두 번째 장면을 시작합니다. 다른 클래스에서 해당 컨트롤러 내의 메서드에 액세스하려고합니다. 새로운 씬 컨트롤러의 핸들을 어떻게 얻을 수 있습니까?새로운 스테이지의 핸들을 얻는 방법 Controller JavaFX

public void startNewScene() throws IOException{ 
    Stage stage = new Stage(); 
    Partent root; 
    root = FXMLLoader.load(getClass().getResource("fxmlfile.fxml"); 
    Scene scene = new Scene(root); 
    Stage.setScene(scene); 
    stage.show(); 

} 

답변

2

은 (대신 정적load(...) 방법을 사용)을 FXMLLoader 인스턴스를 생성하고 그것에서 컨트롤러 얻을 :

public void startNewScene() throws IOException{ 
    Stage stage = new Stage(); 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("fxmlfile.fxml")); 
    Parent root = loader.load(); 
    MyController controller = loader.getController(); 
    Scene scene = new Scene(root); 
    Stage.setScene(scene); 
    stage.show(); 
} 

은 분명히 fxmlfile.fxml에 대한 컨트롤러의 실제 클래스 이름으로 MyController 교체를 .

+0

ClassCastException이 발생했습니다. AnchorPane FXMLLoader로 캐스팅 할 수 없습니다. 또한 BorderPane으로 변경했습니다. 동일한 오류가 FXMLLoader로 캐스팅 될 수 없습니다. – Moe

+0

@Moe 잘못된 코드를 복사했습니다. –

+0

맞습니다! 새 FXMLLoader (getClass()) 대신 FXMLLoader.load (getClass())를 수행했습니다. 너의 도움을위한 thx! – Moe

관련 문제