在控制器之间来回发送数据JavaFX FXML

时间:2018-01-04 23:10:30

标签: java javafx

我在这里找到的所有问题都回答了如何将数据从一个控制器发送到另一个控制器。但是,我不知道如何将第二个控制器中的另一个值返回到第一个控制器。基本上,在第一个控制器仍然打开时来回发送数据

主要课程:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("Screen1.fxml"));
            Scene scene = new Scene(loader.load());
            primaryStage.setScene(scene);
            primaryStage.setTitle("Screen 1");
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

屏幕1控制器:

public class Screen1Controller {
    @FXML
    TextField textFieldScreen1;
    @FXML
    Button buttonScreen1;
    @FXML
    Label labelScreen1;

    @FXML
    private void initialize() {

        buttonScreen1.setOnAction((event) -> {
            try{
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(getClass().getResource("Screen2.fxml"));
                Scene scene = new Scene(loader.load());
                Stage stage = new Stage();

                //Send data to Screen 2
                Screen2Controller controller2 = loader.getController();
                controller2.receiveTextFromScreen1(textFieldScreen1.getText());

                stage.setTitle("Screen 2");
                stage.setScene(scene);
                stage.show();
            }   catch (IOException e) {
                    e.printStackTrace();
                }
        });

    }
}

屏幕2控制器:

public class Screen2Controller {
    @FXML
    TextField textFieldScreen2;
    @FXML
    Button buttonScreen2;
    @FXML
    Label labelScreen2;

    @FXML
    private void initialize() {

        buttonScreen2.setOnAction((event) -> {
            Stage currentStage = (Stage) buttonScreen2.getScene().getWindow();
            currentStage.close();
        });
    }

    public void receiveTextFromScreen1(String stringScreen1){
        labelScreen2.setText(stringScreen1);
    }
}

我尝试使用相同的策略(在Screen1Controller中创建一个方法并在第二个控制器中实例化Screen1Controller),但使用以下代码但它没有用。

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("Screen2.fxml"));
//Send data to Screen 1
Screen1Controller controller1 = loader.getController();
controller1.receiveTextFromScreen2(textFieldScreen2.getText());

另外,与此事有关(我假设)第二个问题:我在每次单击按钮时更改控制器1中的TextField而不创建新窗口时如何更新第二个视图(即使用相同的第二个视图实例)?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

创建构造函数

public Screen1Controller {
Arrow the parameters
}

当您调用控制器时,您将解析参数:

void on2players(ActionEvent event) throws IOException {
    client.out.println("ACTION NEWGAME 2 4");
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/boardscreen.fxml"));
    loader.setController(new Screen1Controller(parameter);
    Parent root = loader.load();
    Scene scene= new Scene(root);
    Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
    window.setScene(scene);
    window.show();
}


public class Screen1Controller implements Initializable {

    public Screen1Controller() {
        //parameters
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    switch(numberOfPlayers){
    case 2:
    //TODO...creating board for 2 players
    case 3:
    //TODO creatinf board for 3 players
   }

    }

如果这样做,则强制fxml加载程序调用控制器。注意:您应该将控制器置于.FXML