Change of frame in javaFX

2

How can I change from one frame to another by closing the first frame in javaFX ?

I have the following code, but it does not work, it does not open the other windows:

Proof Windows Class:

package pruebaventanas;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class PruebaVentanas extends Application {

    private Stage stagePrincipal;
    private AnchorPane rootPane;

    @Override
    public void start(Stage stagePrincipal) throws Exception {
        this.stagePrincipal = stagePrincipal;
        mostrarVentanaPrincipal();

    }

    /*
     * cargamos la ventana principal
     */
    public void mostrarVentanaPrincipal() {
        try {
            FXMLLoader loader = new FXMLLoader(PruebaVentanas.class.getResource("VentanaPrincipal.fxml"));
            rootPane=(AnchorPane) loader.load();
            Scene scene = new Scene(rootPane);
            stagePrincipal.setTitle(".: TEAM :.");
            stagePrincipal.setScene(scene);
            VentanaPrincipalController controller = loader.getController();
            controller.setProgramaPrincipal(this);
            stagePrincipal.show();
        } catch (IOException e) {
        }
    }

    public void mostrarVentanaSecundaria() {
        try {
            FXMLLoader loader = new FXMLLoader(PruebaVentanas.class.getResource("cMenuTutor.fxml"));
            AnchorPane ventanaDos = (AnchorPane) loader.load();
            Stage ventana2 = new Stage();
            ventana2.setTitle("Bienvenido Tutor");
            ventana2.initOwner(stagePrincipal);
            Scene scene = new Scene(ventanaDos);
            ventana2.setScene(scene);
            cMenuTutorController controller = loader.getController();
            controller.setStagePrincipal(ventana2);
            ventana2.show();

        } catch (Exception e) {
        }
    }
    public void mostrarPerfilTutor() {
        try {
           // System.out.println("holla");
            FXMLLoader loader = new FXMLLoader(PruebaVentanas.class.getResource("cPerfilTutor.fxml"));
          // System.out.println("jjja");
            AnchorPane ventanaTres = (AnchorPane) loader.load();
            Stage ventana3 = new Stage();
            ventana3.setTitle("Bienvenido Tutor");
            ventana3.initOwner(stagePrincipal);
            Scene scene3 = new Scene(ventanaTres);
            ventana3.setScene(scene3);
            cPerfilTutorController controller = loader.getController();
            controller.setStagePrincipal(ventana3);
            ventana3.show();
            /**FXMLLoader loader = new FXMLLoader(PruebaVentanas.class.getResource("VentanaPrincipal.fxml"));
            rootPane=(AnchorPane) loader.load();
            Scene scene = new Scene(rootPane);
            stagePrincipal.setTitle(".: TEAM :.");
            stagePrincipal.setScene(scene);
            VentanaPrincipalController controller = loader.getController();
            controller.setProgramaPrincipal(this);
            stagePrincipal.show();*/

        } catch (Exception e) {
        }
    }

    public void mostrarMenuAdministrador() {
        try {
            System.out.println("holla");
            FXMLLoader loader = new FXMLLoader(PruebaVentanas.class.getResource("cMenuAdministrador.fxml"));
            System.out.println("jjja");
            AnchorPane ventanaCuatro = (AnchorPane) loader.load();
            Stage ventana4 = new Stage();
            ventana4.setTitle("Bienvenido Administrador");
            ventana4.initOwner(stagePrincipal);
            Scene scene4 = new Scene(ventanaCuatro);
            ventana4.setScene(scene4);
            cMenuAdministradorController controller = loader.getController();
            controller.setStagePrincipal(ventana4);
            ventana4.show();

        } catch (Exception e) {
        }
    }

    public void mostrarPerfilAdministrador() {
        try {
           // System.out.println("holla");
            FXMLLoader loader = new FXMLLoader(PruebaVentanas.class.getResource("cPerfilAdministrador.fxml"));
          // System.out.println("jjja");
            AnchorPane ventanaCinco = (AnchorPane) loader.load();
            Stage ventana5 = new Stage();
            ventana5.setTitle("Bienvenido Administrador");
            ventana5.initOwner(stagePrincipal);
            Scene scene5 = new Scene(ventanaCinco);
            ventana5.setScene(scene5);
            cPerfilAdministradorController controller = loader.getController();
            controller.setStagePrincipal(ventana5);
            ventana5.show();

        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="350" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="pruebaventanas.VentanaPrincipalController">
    <children>
        <Label layoutX="100" layoutY="20" minWidth="120" minHeight="25" fx:id="label" text="HyperGerät Presenta"  />
        <Label layoutX="135" layoutY="45" minWidth="120" minHeight="25" fx:id="labelt" text=" TEAM"  />
        <TextField layoutX="120" layoutY="120" minWidth="100" minHeight="25" fx:id="usuario"    />
        <TextField layoutX="120" layoutY="160" minWidth="100" minHeight="25" fx:id="contrasena"    />
        <Label layoutX="50" layoutY="120" minHeight="16" minWidth="69" fx:id="label2" text="Usuario"/>
        <Label layoutX="50" layoutY="160" minHeight="16" minWidth="69" fx:id="label3" text="Contraseña"/>
        <Button layoutX="126" layoutY="235" text="Abrir Ventana" onAction="#ventanaNew" fx:id="button" />
    </children>
</AnchorPane>

WindowPrincipalController class:

package pruebaventanas;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class VentanaPrincipalController implements Initializable {

    private PruebaVentanas ProgramaPrincipal;

    @FXML
    private void ventanaNew(ActionEvent event) {
        System.out.println("Entra boton");
        validaUsuario();
        ProgramaPrincipal.mostrarMenuAdministrador();

    }

    public void setProgramaPrincipal(PruebaVentanas ProgramaPrincipal) {
        this.ProgramaPrincipal = ProgramaPrincipal;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    private void validaUsuario(){

    }
}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="350" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="pruebaventanas.cMenuTutorController">
<children>
        <Button layoutX="126" layoutY="120" text="Mi Perfil" onAction="#perfilTutor" fx:id="perfil" />
        <Button layoutX="126" layoutY="160" text="Actividades" onAction="#salirVentana" fx:id="actividades" />
        <Button layoutX="200" layoutY="280" text="Cerrar  Sesion" onAction="#salirVentana" fx:id="cerrarSesion" />
    </children>
</AnchorPane>

Class cMenuTutorController:

package pruebaventanas;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.stage.Stage;

public class cMenuTutorController implements Initializable {
    private Stage stagePrincipal;
    private PruebaVentanas prueba = new PruebaVentanas();

    public void setStagePrincipal(Stage stagePrincipal) {
        this.stagePrincipal = stagePrincipal;
    }

    @FXML
    private void salirVentana(ActionEvent event) {
        stagePrincipal.close();
    }

    @FXML
    private void perfilTutor(ActionEvent event) {
        try{
            System.out.println("Cambia Algo");
            prueba.mostrarPerfilTutor();
        }
        catch(Exception e){
            String xD = e.getMessage();
            System.out.println(xD);
        }

    }

    @FXML
    private void menuTutorAdmin(ActionEvent event) {
        try{
            System.out.println("Cambia Algo");
            prueba.mostrarPerfilTutor();
        }
        catch(Exception e){
            String xD = e.getMessage();
            System.out.println(xD);
        }

    }


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pruebaventanas.cPerfilAdministradorController">
    <stylesheets>
        <URL value="@ventanados.css"/>
    </stylesheets>
    <children>
            <Button layoutX="25" layoutY="125" fx:id="consultar"   text="Consultar mi Informacion"  />
            <Button layoutX="25" layoutY="200" fx:id="modifica" text="Modificar Informacion"  />
            <Button layoutX="25" layoutY="300" fx:id="atras" text="⬅"  />
    </children>
</AnchorPane>

Class cProfileAdministratorController:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pruebaventanas;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author Otoniel Aguirre
 */
public class cPerfilAdministradorController implements Initializable {

    private Stage stagePrincipal;

    public void setStagePrincipal(Stage stagePrincipal) {
        this.stagePrincipal = stagePrincipal;
    }
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pruebaventanas.cPerfilTutorController">
    <stylesheets>
        <URL value="@ventanados.css"/>
    </stylesheets>
    <children>
            <Button layoutX="25" layoutY="125" fx:id="consulta"   text="Consultar mi Informacion"  />
            <Button layoutX="25" layoutY="200" fx:id="modificar" text="Modificar Informacion"  />
            <Button layoutX="25" layoutY="300" fx:id="atras" text="⬅"  />
    </children>
</AnchorPane>

Class cProfileTutorController:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author Otoniel Aguirre
 */
public class cPerfilTutorController implements Initializable {
    private Stage stagePrincipal;

    public void setStagePrincipal(Stage stagePrincipal) {
        this.stagePrincipal = stagePrincipal;
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

The problem is that it does not open the next window, nor does it show me anything.

    
asked by Sandra Green 15.11.2016 в 17:50
source

2 answers

2

Try doing it this way, for example in the showMenuAdministrator function that jumps to the next window after clicking the open window button.

public void mostrarMenuAdministrador(Event event) {
    try {

        Parent root = FXMLLoader.load(getClass().getResource("cMenuTutor.fxml"));
        Scene scene = new Scene(root);
        Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        appStage.setScene(scene);
        appStage.toFront();
        appStage.show();

    } catch (Exception e) {
    }
}

note that you add as an argument of the function the event you pass to get the Stage.

in the other functions that do the same thing in the TestVistanas class, you should do it like that.

Notice that in the window VentanaPrincipalController the windowNew function would look like this.

@FXML
private void ventanaNew(ActionEvent event) throws IOException {
    System.out.println("Entra boton");
    validaUsuario();

    ProgramaPrincipal.mostrarMenuAdministrador(event);

}

you must pass as an argument the event you capture by pressing the button open window. You just have to worry about the dimensions of the windows and so on, in JavaFX there are two ways to change the view, one is to change the panels without modifying the window and another is to change the window directly.

It also includes the option to change the window in the controllers and not always in a main class, it can be less cumbersome when it comes to window logic.

    
answered by 13.03.2018 в 02:20
1

I suggest you reuse your Stage to avoid the creation of new Stages, what you can do is re-inject your FXML with FXMLLoader.

Try using this method:

public class AppController extends Application {

    @FXML
    private AnchorPane primaryWindow;

    @Override
    public void start(Stage primaryStage) {
        try {
             Stage primaryStage = (Stage) primaryWindow.getScene().getWindow();
             primaryStage.setScene(loadView("Reservation", "reservationStyles"));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

   //Si deseas hacer alguna enumeracion para tus vistas o 
   //un array con las vistas que tienes creadas es mucho mas legible y facil de reutilizar.
   public static Scene loadView(String viewName, String cssFile) {
       Parent root = FXMLLoader.load(AppController.class.getClass().getResource(viewName + ".fxml"));
                Scene scene = new Scene(root);
                scene.getStylesheets().add(PlayListController.class.getClass()
                        .getResource(cssFile + ".css").toExternalForm());
                return scene;
   }

 }

I hope it will help you. Greetings.

    
answered by 18.01.2019 в 20:12