Multiples stage with javaFX

0

Hello, this question is very simple but I'm starting with JavaFx and I caught on an issue, for example, main class with the main screen where it will have a button that when clicked will open another stage. I have to have a controller for each screen, even there, but I do not want to centralize all the call of the stage in main, in which case I have to create another class for the secondary stage? It's kind of confusing for me so I could not explain it very well. I already understood every concept of when it's just a stage, I even do the layout with FXML to make it easier. If you have any examples to explain, I will be grateful. Thank you all.

    
asked by anonymous 18.05.2016 / 15:05

2 answers

1

I usually use a class Singleton to work with Stage . So to switch between screens in controllers I call the loadNewStage method of my class responsible for working Stage .

HaveStage Class

public class HaveStage {

    private static HaveStage haveStage = null;
    private Stage stage;

    private HaveStage(Stage stage) {
        this.stage = stage;
    }

    public static HaveStage instance(Stage stage) {
        if (haveStage == null) {
            haveStage = new HaveStage(stage);
        }
        return haveStage;
    }

    public Stage getStage() {
        return this.stage;
    }

    public void loadNewStage(Parent fxmlLoad) {
        if (stage != null) {
            Parent root = fxmlLoad;
            Scene scene = new Scene(root);
            stage.setScene(scene);
            //stage.show();
        }
    }
}

Application Class

public class MinhaClasse extends Application {
    private HaveStage haveStage;

    @Override
    public void start(Stage stage) {
        this.haveStage = HaveStage.instance(stage);
        Parent root = null;
        Scene scene;

        try {
            root = FXMLLoader.load(getClass().getResource("/br/com/projeto/primeiraTela.fxml"));
        } catch (IOException ex) {
            Logger.getLogger(MinhaClasse.class.getName()).log(Level.SEVERE, null, ex);
        }

        scene = new Scene(root);

        haveStage.getStage().setScene(scene);
        haveStage.getStage().show();
    }
}

Whenever you want to switch the screen call the loadNewStage method on the controllers.

Example Driver

public class LoginScreenController {
    @FXML
    void login(InputEvent event) {
        try {
            Parent temp = FXMLLoader.load(getClass().getResource("/br/com/projeto/segundaTela.fxml"));
            changeScreen(temp);

        } catch (IOException ex) {
            ex.getCause().printStackTrace();
            new Notifications().errorNotification("Erro ao carregar nova tela!");
        }
    }

    private void changeScreen(Parent fxmlLoad) {
        HaveStage.instance(null).loadNewStage(fxmlLoad);
    }
}
    
19.05.2016 / 23:53
-1

Hello, I recently created a utility class to abstract screen creation in JavaFX, the installation is simple and I use it as well. Basically what you should do is to extend your WindowControllerFx controller, override the getFXML method where you inform the path of your layout, finally instantiate the class and call one of the show (show (), showAsDialog () method options ... ).

The lib is here

    
18.03.2017 / 18:12