Multiples Tage with javafx

Asked

Viewed 985 times

0

Good morning, this question is very simple but I’m starting with Javafx and locked in a question, for example, class main with the main screen where will have a button that when clicked will open another Stage. I have to have a controller for each screen, until then quiet, but I do not want to centralize every call of the Stage in the main, in which case I have to create another class for the secondary Stage? It’s a little confusing for me so I couldn’t explain it very well. I already understood the whole concept of when it is just a Stage, including FXML layout for easy. In case you have any examples to give me, I’d be grateful. Grateful to all.

  • Hi @Ronaldo Alves. It is beneficial for the community to give a feedback if the problem has been solved or if it still remains. I left an answer but you did not give a return.

2 answers

1

I usually use a class Singleton to work the Stage. So to switch between screens in the controllers call the method loadNewStage of my class responsible for working the 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 method loadNewStage in the controllers.

Controller Example

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);
    }
}

-1

Hello, I recently created a utility class to abstract the creation of screens in Javafx, the installation is simple and the use too. Basically what should be done 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 method options (show(), showAsDialog()...).

The lib is here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.