JavaFX FXML参数从控制器A传递到B并返回 [英] JavaFX FXML Parameter passing from Controller A to B and back

查看:246
本文介绍了JavaFX FXML参数从控制器A传递到B并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个由多个控制器组成的基于控制器的JavaFX GUI.

I want to create a controller based JavaFX GUI consisting of multiple controllers.

我无法完成的任务是将参数从一个场景传递到另一个场景并返回.

The task I can't accomplish is to pass parameters from one Scene to another AND back.

或者换句话说: MainController加载SubController的fxml,将一个对象传递给SubController,切换场景.不应有两个打开的窗口. 工作完成后,SubController应将场景切换回MainController并传递一些对象. 这就是我失败的地方.

Or in other words: The MainController loads SubController's fxml, passes an object to SubController, switches the scene. There shall not be two open windows. After it's work is done, the SubController shall then switch the scene back to the MainController and pass some object back. This is where I fail.

这个问题与这个问题非常相似,但仍未得到解答. 传递参数JavaFX FXML 评论中还提到了它:

This question is very similar to this one but still unanswered. Passing Parameters JavaFX FXML It was also mentioned in the comments:

当您将参数从第一个控制器传递到第二个控制器,但是如何将参数从第二个控制器传递到第一个控制器时,这是有效的,这意味着在first.fxml加载之后.

"This work when you pass parameter from first controller to second but how to pass parameter from second to first controller,i mean after first.fxml was loaded.

– Xlint Xms 17年9月18日在23:15"

– Xlint Xms Sep 18 '17 at 23:15"

我在该线程的最高答案中使用了第一种方法.

I used the first approach in the top answer of that thread.

有人知道如何在没有外部库的情况下实现这一目标吗?

Does anyone have a clue how to achieve this without external libs?

推荐答案

有很多方法可以做到这一点.

There are numerous ways to do this.

这是一种解决方案,它传递了消费者到另一个控制器.一旦完成工作,另一个控制器便可以调用使用者以接受结果.该示例基于示例代码,该示例代码来自对所链接问题的答案.

Here is one solution, which passes a Consumer to another controller. The other controller can invoke the consumer to accept the result once it has completed its work. The sample is based on the example code from an answer to the question that you linked.

public Stage showCustomerDialog(Customer customer) {
  FXMLLoader loader = new FXMLLoader(
    getClass().getResource(
      "customerDialog.fxml"
    )
  );

  Stage stage = new Stage(StageStyle.DECORATED);
  stage.setScene(
    new Scene(
      (Pane) loader.load()
    )
  );

  Consumer<CustomerInteractionResult> onComplete = result -> {
    // update main screen based upon result.
  };
  CustomerDialogController controller = 
    loader.<CustomerDialogController>getController();
  controller.initData(customer, onComplete);

  stage.show();

  return stage;
}

...

class CustomerDialogController() {
  @FXML private Label customerName;
  private Consumer<CustomerInteractionResult> onComplete
  void initialize() {}
  void initData(Customer customer, Consumer<CustomerInteractionResult> onComplete) {
    customerName.setText(customer.getName());
    this.onComplete = onComplete;
  }

  @FXML
  void onSomeInteractionLikeCloseDialog(ActionEvent event) {
    onComplete.accept(new CustomerInteractionResult(someDataGatheredByDialog));
  }
}

执行此操作的另一种方法是将result属性添加到对话框屏幕的控制器,感兴趣的调用者可以侦听或检索result属性. 结果属性是内置JavaFX对话框的工作方式,因此您实际上将在模仿其中的某些功能.

Another way to do this is to add a result property to the controller of the dialog screen and interested invokers could listen to or retrieve the result property. A result property is how the in-built JavaFX dialogs work, so you would be essentially imitating some of that functionality.

如果您经常进行这种来回传递,那么可以使用基于胶子点燃,可能会为您提供帮助.

If you have a lot of this passing back and forth stuff going on, a shared dependency injection model based on something like Gluon Ignite, might assist you.

这篇关于JavaFX FXML参数从控制器A传递到B并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆