Pass an object between scenes (screens) JavaFx

3

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:

TheideaistoclickonabuttonintheClimaviewandbeforeopeningthenextview,instantiatePerfilandthencallthenextview.

Profile:

publicclassPerfil{intclima;intpaisagem;

ThemethodI'musingtocallthenextview,thiswillbecalledbyclickingontheClimaviewbutton.

@FXMLprivatevoidhandlePaisagemCalor(ActionEventevent)throwsIOException{Perfilperfil=newPerfil();perfil.setClima(1);perfil.setPaisagem(0);ParentclimaParent=FXMLLoader.load(getClass().getResource("/lisp/view/PaisagemCalor.fxml"));
        Scene scene = new Scene(climaParent);
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        stage.setScene(scene);
        stage.show();
    }
    
asked by anonymous 02.11.2017 / 01:57

1 answer

1

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:

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

   perfil.setClima(1);
   perfil.setPaisagem(0);

   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()));
   stage.show();
}
    
09.11.2017 / 14:20