JavaFX + Scene Builder how switch scene

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I'm working with JavaFx and Scenebuilder and want create a local app for myself called "Taskplanner" in eclipse.

I created a new Stage and set it with a Scene (see Main.java). But not sure how to set a new Scene in the old stage (see Controller.java). Didnt also not find out if it is possible pass the signInButtonClicked()-Methode the "Stage primaryStage" over Scene Builder

Can anybody help ?

Controller.java:

@FXML Button btnSignIn;  @FXML public void signInButtonClicked() throws Exception { //Here I want call the new Scene(SignInGUI.fxml) in my old Stage    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/SignInGUI.fxml")); } 

Main.java:

        @Override         public void start(Stage primaryStage) throws Exception        {             Parent root = FXMLLoader.load(getClass().getResource("../view/LoginGUI.fxml"));              primaryStage.setTitle("Taskplanner");             primaryStage.setScene(new Scene(root,500,500));             primaryStage.show();         }           public static void main(String[] args) {              launch(args);     } 

回答1:

You can get a reference to the Scene and Window from your button reference. From there, it's up to you to decide how to you want to show the new view.

Here's how you get those references:

Scene scene = btnSignIn.getScene(); Window window = scene.getWindow(); Stage stage = (Stage) window; 

You can change the view by changing the root of your Scene:

FXMLLoader loader = ... // create and load() view btnSignIn.getScene().setRoot(loader.getRoot()); 

Or you can change the entire Scene:

FXMLLoader loader = ... // create and load() view Stage stage = (Stage) btnSignIn.getScene().getWindow(); Scene scene = new Scene(loader.getRoot()); stage.setScene(scene); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!