How can I send data from one controller to another in JavaFx?

0

I need to send parameters to the different controllers that I use, this to maintain a certain series of validations with this information, I have managed to find some examples on the internet, but these are too unstable causing exceptions in lots and sometimes it is impossible to use the information that I send to other controllers. This is the example I found but I would like to know if there is a more feasible way to do what I want:

    
asked by Isaac Perez 22.11.2018 в 02:10
source

1 answer

0

Well, again nobody in this community has been able to solve my doubt and I have found a solution on my own. It turns out that problems are generated when using set methods stored in the second controller since it is impossible to build this object before using these methods (according to some examples found on the internet), the solution to all this is to use the function "FXMLLoader" "load ()" this will allow to build the second controller and consequently we will be able to make use of its defined methods "set" or any other type of method. Here is an example:

loader.setLocation(getClass().getResource("FXMLDocument.fxml"));
loader.load();
SegundoController document = loader.getController();
document.setEmployee(new Empleado(5,"Hola"));
Parent p = loader.getRoot();
Stage s = new Stage();
s.setScene(new Scene(p));
s.show();

Now from the second controller we can make use of the information received.

    
answered by 22.11.2018 / 03:59
source