Pass an object between Javafx (screens)

Asked

Viewed 1,534 times

3

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

I have an object that will be filled in different screens of the system, my question is when to pass this object through the Cenes. I have the following structure:

Estrutura do sistema

The idea is to click a button in the view Clima and before opening the next view, instantiate Perfil and then call the next view.

Profile:

public class Perfil {
int clima;
int paisagem;

The method I am using to call the next view, this will be called by clicking the view button Clima.

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

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

        Parent climaParent = 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();
    }
  • This is one of the main disadvantages of FXML, navigation between screens. This can only be solved with static variables. I suggest you open the task manager and try changing the screens to see the impact on memory consumption.

  • 1

    Actually it can be done using the controller class constructor, communication and information exchange between two Scenes can be done this way. Don’t necessarily have to use Static variables

  • The class FXMLLoader has the method .setController(ControllerClasse), use it to pass the Scene Controller.

1 answer

1

Remove the tag fx:controller of your fxml file (if you are using it that way). Then please prompt the controller "manually" at the time you load fxml.

From what I understand, you want to pass an object Perfil to the controller PaisagemCalorController. So the first thing to do is to create a constructor in this control class so that you receive 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.