Pass data between windows with javafx

0

I am working with a form, which has several textFields that I want to fill by taking the data from another window, the window from which I want to take the data I open it by clicking on an icon that is located next to the textField

The second window I open when I click on the book that is on the left side of the textField Provider, in which I only have one tableView and a "search engine"

from which I want to bring the provider's name to me by clicking on the record in the table, and that in turn the second window is closed

This is the code I have when I click on the icon:

 @FXML
void seleccionarProveedor(MouseEvent event) {
    try {
        Stage proveedores = new Stage();
        FXMLLoader loader = new FXMLLoader();
        AnchorPane root = (AnchorPane)loader.load(getClass().getResource("/interfaces/seleccionarProveedor.fxml"));
        Scene scene = new Scene(root);
        proveedores.setScene(scene);
        proveedores.alwaysOnTopProperty();
        proveedores.initModality(Modality.APPLICATION_MODAL);
        proveedores.show();
    } catch (IOException ex) {
        Logger.getLogger(InicioController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

And this one that I have when I click on any record in the table that is in the second window:

    @FXML
void seleccionarProveedor(MouseEvent event) {
   String nombreProveedor = tblData.getSelectionModel().getSelectedItem().getNombre();
}

It is the first time that I do an application with interface. I still do not quite understand the view controller mode that JavaFx handles.

    
asked by Johnatan De Leon 28.10.2018 в 02:09
source

2 answers

0

The easiest way would be to use a multi-use variable of the static or serializable type. It could be a List, Hash, map, among others that allows you to save data of interest without any problem. The other would be that when loading the fxml before being placed in the scene locate the nodes of interest and load it with the data you need to show.

 Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));
    // Justo aqui debes hacer el cambio antes de ser mostrado. Recuerda que los nodos de interes deben tener su id. El root contiene los nodos hijos, haz un ciclo iterativo para encontrarlos y luego como son nodos tienes que hacerle un cast
    //ejm; TextField campo = (TextField) nodoEncontrado;

    stage.setTitle("FXML Welcome");
    stage.setScene(new Scene(root, 300, 275));
    stage.show();
    
answered by 26.11.2018 в 19:24
0

The "optimal" or correct would be to have an EventBus in your application. That the selection screen of the table, when selecting a provider, would make an EventBus.notifyEvent (Event.SELECTION_PROVIDER, selection.getName ()) and your other window would have an EventBus.registerListener (Event.SELECTION_PROVIDER, (String selection) - > {textfield.setText (selection)}. All this that I put to you is pseudocode but I hope that it will serve as a guide / help to reach a solution. You can have an Enum Event with all the events that you need. I use it in my applications and it is very easy for me to communicate between unrelated screens and it's a very clean solution.

    
answered by 29.11.2018 в 20:22