JavaFX-将子参数解析回父级

时间:2018-08-23 18:53:12

标签: button javafx controller window

我有2个控制器,我想将值从第二个控制器传递到第一个控制器,这是代码示例:

    FXMLController.java
        private int paramAnswers;
        private int paramNotificationTime;
        private int paramNotificationDelay;

            @FXML
            private void handleMenuItemOptionsAction(ActionEvent event) throws IOException{
                Parent root = FXMLLoader.load(getClass().getResource("/fxml/Options.fxml")); // UNDECORATED*

                Scene scene = new Scene(root, Color.TRANSPARENT);
                final Stage stage = new Stage();
                stage.setTitle("Options");
                stage.setScene(scene);  
                stage.show();
        }

public void setOptionsParams(int paramAnswers, int paramNotificationTime, int paramNotificationDelay){
        this.paramAnswers = paramAnswers;
        this.paramNotificationTime = paramNotificationTime;
        this.paramNotificationDelay = paramNotificationDelay;
    }

和第二个控制器:

    OptionsController.java
    private FXMLController parentController;
    private int paramAnswers;
        private int paramNotificationTime;
        private int paramNotificationDelay;

    @Override
        public void initialize(URL location, ResourceBundle resources) { 
    .... }

    @FXML
        private void handleButtonSaveAction(ActionEvent event) throws IOException{
            /*Pass these parameteres OptionsController parameters back to the FXMLController like parentController.setOptionsParams(paramAnswers, paramNotificationTime, paramNotificationDelay);
*/
(((Button)event.getSource()).getScene().getWindow())).close();

        } 

Arleady尝试将FXMLControler解析为.this到OptionsController的initialize方法中,试图在Listoverflow上使列表器和其他问题解决,但只是不希望工作:<我需要将归因给FXMLController并关闭子窗口,所以我的主应用程序将根据传递的值来更改行为...:X 对于任何帮助,我将不胜感激

2 个答案:

答案 0 :(得分:0)

您可以在第二个控制器中有一个函数,假设passParams()设置它的参数,当您单击按钮或其他内容时,您想传递给该控制器以及从第一个控制器传递的所有参数
这行

FXMLLoader.load(getClass().getResource("/fxml/Options.fxml"));

需要更改为

FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Options.fxml"));
Parent root = (Parent) loader.load();
OptionsController controller = loader.getController();

编辑 您需要将第一个控制器传递给第二个控制器

controller.setParentController(this); // this is the first controller

在第二个控制器中

FirstController mController;
public void setParams(FirstController controller) {
    this.mController = controller;
}

现在在按钮单击功能中,您将使用从上一步获得的mController

mController.setOptionsParams(...); //send the params collected from the textfields

此功能在FirstController中实现
注意:使用回调的更通用方法是相同的,但是您的代码通过在FirstController中实现具有setOptionParams()方法的通用接口,而依赖于接口而不是具体的类 < / p>

答案 1 :(得分:-1)

首先,向您的父控制器类添加一个回调接口:

public interface OptionCallback {
     public void setOptionsParams(int paramAnswers, int paramNotificationTime, int paramNotificationDelay);
 }

 public class YourParentController implements Initializable, OptionCallback {
   ...
 }    

使用FXMLLoader对象,您可以保留子控制器对象并将父对象传递给它:

FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("yourFXML.fxml"));

Parent root = (Parent) loader.load();

YourChildController yourChildController = loader.<YourChildController>getController();

yourChildConrtoller.registerCallback(this);

在childController中,保存该回调:

private OptionCallback optionCallback;

public void registerCallback(OptionCallback callback) {
    optionCallback = callback;
}

只要准备好结果,就可以将其传递给父级:

optionCallback.setOptionsParams(...);