Handle change between Scenes. JAVAFX without FXML

1

My main class has the Stage field. I have created classes that extend from some Node (VBox, BorderPane, etc.) whose constructors create and establish the content of themselves. A "Follow" button is part of that content.

Example:

public class ClasePrincipal extends Application {
    Stage stage;
    public void start(Stage primaryStage){
        stage = primaryStage;
        stage.show();
    }
}

public class PrimeraPantalla entends VBox {
    public PrimeraPantalla(){
        /* Contenido como cajas de texto, combobox, cualquier cosa */
        Button seguir = new Button("SEGUIR");
        this.getChildren.add(seguir);
        seguir.setOnAction(e->{ /* Codigo */});
        /* Tal vez más contenido */
    }
}

public class SegundaPantalla entends HBox{
    public SegundaPantalla(){
        /* Contenido como cajas de texto, combobox, cualquier cosa */
        Button avanzar = new Button("AVANZAR");
        this.getChildren.add(avanzar);
        avanzar.setOnAction(e->{ /* Codigo */});
        /* Tal vez más contenido */
    }
}

Things like creating static methods in the main class have worked for me:

public static void setSegundoScene(){
    SegundaPantalla segundaPantalla = new SegundaPantalla();
    Scene scene = new Scene(segundaPantalla,500,500);
    stage.setScene(scene);
}

Which are called from the Screen classes by clicking on the Follow / Advance buttons.

But I would like to know better ways to do it. If that means changing my code a lot (although there is not so much code to change that we say) it does not matter.

I have seen several videos in which the different Scenes are in the main class or others in which they use FXML. It's not what I'm looking for.

Thank you very much everyone.

    
asked by Xavier Figueroa 18.03.2018 в 02:29
source

0 answers