Passing parameters between two JavaFX controllers

前端 未结 3 1186
不知归路
不知归路 2020-12-06 08:37

I want to click a column and send the cell index to a new stage. But I can\'t pass my parameter (int clickIndex) to another controller EditClientControlle

3条回答
  •  -上瘾入骨i
    2020-12-06 09:03

    When moving from one controller to another(one view to another), you could initialize the EditClientController with the required parameters

    Eg:

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("editClient.fxml"));
    EditClientController ctrl = new EditClientController();
    ctrl.setCellIndex(id);
    fxmlLoader.setController(ctrl);
    

    If you have specified the controller in the fxml file, the you could use:

    editWrapper.getCurrentController().setCellIndex(id);
    

    where editControllerWrapper is a class that loads the new view and has a method getCurrentController that returns an instance of javafx controller.

    Eg: public class EditControllerWrapper {

    private Parent root;
    
    
    public EditControllerWrapper () {
        try
        {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("editClient.fxml"),                           
            ...
        } catch (Exception e) {
            ...
        }
    }
    
    
    public  T getCurrentController ()  {
        return loader.getController();
    }
    

提交回复
热议问题