Pass an object between scenes (screens) JavaFx

Posted on

Question :

The idea of the system is to save some information in a txt file that will be queried by a code in Lisp, a decision tree routine will be performed and will return the result to be displayed in the interface in Java.

I have an object that will be populated in different screens of the system, my doubt is the time to pass this object through the scenes. I have the following structure:





        Scene scene = new Scene(climaParent);
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();


Answer :

Remove the fx:controller tag from your fxml file (if you’re using it that way). Then instantiate the driver “manually” the moment you load the fxml.

As far as I understand, you want to pass a Perfil object to the PaisagemCalorController driver. So the first thing to do is to create a constructor in this control class so that it gets a Perfil :

class PaisagemCalorController {

    private Perfil perfil;

    public PaisagemCalorController(Perfil perfil){
        this.perfil = perfil;

    /* outros métodos e atributos. */

Then your method would look like this:

private void handlePaisagemCalor(ActionEvent event) throws IOException{
   Perfil perfil= new Perfil();


   FXMLLoader fxmlloader = new FXMLLoader(getClass().getResource("/lisp/view/PaisagemCalor.fxml"));
   // Definindo quem é o controller desse 'fxml':
   fxmlloader.setController(new PaisagemCalorController(perfil));

   Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

   // Carregando o fxml na scene:
   stage.setScene(new Scene(fxmlloader.load()));;


Leave a Reply

Your email address will not be published. Required fields are marked *