Open window from a MenuItem in JavaFX

0

I'm learning to move around in the JavaFX environment and I've already done all the I need windows, but I still can not connect them to each other. That is, for example, from the initial window VentanaMaestra.fxml touch in the menu bar, touch Autos, and thus call the window AutosVentana.fxml This is the VentanaMaestraController.java

package pantallas;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

/**
 *
 * @author leandrocotarelo
 */
public class VentanaMaestraController implements Initializable {

    @FXML
    private javafx.scene.control.MenuItem exit;
    @FXML
    private javafx.scene.control.MenuItem btn_Autos;

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

    @FXML
    public void cerrarApp(javafx.event.ActionEvent event) {
        Platform.exit();
    }


    }      


Este es el AutosVentanaController.java'package pantallas;

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

/**
 * FXML Controller class
 *
 * @author leandrocotarelo
 */
public class AutosVentanaController implements Initializable {

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

}
    
asked by Leandro Cotarelo 30.12.2018 в 17:05
source

1 answer

0

To do this you must create a new window and close the previous one.

To unfold the new window you can use these methods:

public Parent cargarFXML(String rutaFXML) {

    Parent root = null;

    try {
       root = FXMLLoader.load(getClass().getResource(rutaFXML));

    } catch (IOException ex) {
        Logger.getLogger(VentanaImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    return root;
}

 public void nuevaVentana(String rutaFXML){
   Stage stage = new Stage();
   stage.initStyle(StageStyle.TRANSPARENT);


   Scene scene = new Scene(this.cargarFXML(rutaFXML));
   scene.setFill(null);

   stage.setTitle("Sistema para el Tratamiento de Datos XML");
   stage.getIcons().add(new Image(getClass().getResource("/stdxml/recursos/icono-stdxml250.png").toExternalForm()));


   stage.setScene(scene);
   stage.setResizable(false);
   stage.sizeToScene();
   stage.show();
}

To close the previous window you can apply this method

public void cerrarVentana(Node nodo){
   Stage stg = (Stage) nodo.getScene().getWindow();
   stg.close();
}

and to execute them I'll give you another example:

Platform.runLater(new Runnable() {
                     @Override
                     public void run() {
                         Ventana ventana = new VentanaImpl();
                         ventana.nuevaVentana("/stdxml/vista/Dashboard.fxml");

                         ventana.cerrarVentana(splash);

                     }
                 });
    
answered by 01.01.2019 / 22:27
source