1

I've been developing an application with following structure: App structure

It has a main.fxml (menubar and contains anchorpane), the anchorpane is changed in it content by vistanavigator class that call maincontroller and pass the content1.fxml loaded as a node.

I want to pass parameters between content views when the view is changed from content 1 to content 2, i've been trying how i can do this but i dont know how.

Here is the code:

Main Class

public class Main extends Application {



@Override
public void start(Stage stage) throws Exception{
    FXMLLoader loader2 = new FXMLLoader();
    Pane ventanaprincipal = (Pane) loader2.load(getClass().getResourceAsStream(VistaNavigator.MAIN));
    Scene scene = new Scene(ventanaprincipal);

    MainController mainController = loader2.getController();
    VistaNavigator.setMainController(mainController);
    VistaNavigator.loadVista(VistaNavigator.VISTA_SESION);
}

public static void main(String[] args) {
    launch(args);
}}

MainController

public class MainController implements Initializable {

    @FXML
    private AnchorPane contenido;


@Override
public void initialize(URL url, ResourceBundle rb) {

}


public void setVista(Node node) {
    contenido.getChildren().setAll(node);

}}

VistaNavigator class

public class VistaNavigator {


public static final String MAIN    = "main.fxml";
public static final String VISTA_SESION = "sesion.fxml";
public static final String VISTA_INTERFAZ = "interfaz.fxml";

private static MainController mainController;

public static void setMainController(MainController mainController) { 
    VistaNavigator.mainController = mainController;
}

public static void loadVista(String fxml) {
    try {
        FXMLLoader loader = new FXMLLoader(VistaNavigator.class.getResource(fxml));
        Node ventana=(Node) loader.load();
        mainController.setVista(ventana);
    } catch (IOException e) {
        e.printStackTrace();
    }
}}

Content1 controller

public class sessioncontroller implements Initializable {



@FXML
private TextField usuario;
@FXML
private PasswordField contrasena;
@FXML
private Button iniciobutton;
@FXML
private Label error;


@Override
public void initialize(URL url, ResourceBundle rb) {



}

@FXML
private void inicio(ActionEvent event){


    if(sesion.getusr()!=null && sesion.getemail()!=null) {

            ////HERE I WANT TO PASS VALUES TO CONTENT2.FXML
            VistaNavigator.loadVista(VistaNavigator.VISTA_INTERFAZ);

    }else {
        error.setVisible(true);
    }

}
}

The question is different than other because the content are changed in same scene as children anchorpane.

  • 2
    This has been answered hundreds of times already; please use the search feature. – Zephyr Jun 19 '18 at 12:36
  • I've checked that post previously, my question is different because the views are not loaded in differents scenes, are loaded in the same scene changing children anchor on main.fxml. I've already tried that solution related on post but i got nullpointexception error. – Adrian Barea Jun 19 '18 at 13:00
  • It may not be a direct answer for your specific code, but the concepts explained should help you! :) – Zephyr Jun 19 '18 at 13:15

0 Answers0