作为子级加载的控制器之间的通讯

时间:2018-06-19 12:27:25

标签: java javafx javafx-8 javafx-2 fxml

我一直在开发具有以下结构的应用程序: App structure

它具有main.fxml(菜单并包含锚定窗格),通过调用maincontroller并传递作为节点加载的content1.fxml的vistanavigator类在其内容中更改了锚定窗格。

当视图从内容1更改为内容2时,我想在内容视图之间传递参数,我一直在尝试如何做到这一点,但是我不知道如何。

代码如下:

主类

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类

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控制器

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);
    }

}
}

问题与其他问题不同,因为内容在与子锚窗格相同的场景中发生了变化。

0 个答案:

没有答案