Javafx 将参数和值从一个 Controller 传递到另一个 Controller

标签 javafx controller

我是 JavaFx 的新手,因此找不到解决我问题的方法

假设我有以下应用程序结构:

- views
      - first.fxml -> this has a button called btnSend and a textfield called txtEnter
      - second.fxml -> this has a textarea called txtView
- Controller 
      - FirstController  -> controller for First
      - SecondController -> controller for second
- Modal
      - AppModal -> here I have a getter and a setter method , 
                    as getText() and setText(String text)

- App
     - Main.java -> This one used FXMLLoader to load first.fxml and second.fxml together.

在 SecondController 中显示从 FirstController 传递的文本的最佳/最佳方式是什么。我的意思是,我在 txtEnter 中输入文本,然后按下按钮 btnSend ,按下按钮后我希望文本显示在 txtView 中正在使用另一个 Controller 。 我已经阅读了很多关于 observers 模式JavaFX properties 可以用来解决这个问题的文章,但不幸的是我无法实现一个有效的解决方案。

如果各位专家能在这方面帮助我,我将不胜感激。我知道它不正确但是任何人都可以给我一个上述项目结构的工作解决方案。

提前致谢。

最佳答案

使用可观察的 StringProperty在模型中:

public class AppModel {

    private final StringProperty text = new SimpleStringProperty();

    public StringProperty textProperty() {
        return text ;
    }

    public final String getText() {
        return textProperty().get();
    }

    public final void setText(String text) {
        textProperty().set(text);
    }
}

让您的 Controller 可以访问模型:

public class FirstController {

    private final AppModel model ;

    @FXML
    private TextField textEnter ;

    public FirstController(AppModel model) {
        this.model = model ;
    }

    // action event handler for button:
    @FXML
    private void sendText() { 
        model.setText(textEnter.getText());
    }
}

public class SecondController {

    private final AppModel model ;

    @FXML
    private TextArea txtView ;

    public SecondController(AppModel model) {
        this.model = model ;
    }

    public void initialize() {
        // update text area if text in model changes:
        model.textProperty().addListener((obs, oldText, newText) -> 
            txtView.setText(newText));
    }
}

现在有点棘手的部分是 Controller 没有无参数构造函数,这意味着 FXMLLoader 的默认机制创建它们是行不通的。最简单的方法是手动设置它们。 同时删除 <fx:controller> FXML 文件中的属性,然后在您的 Main 中类做

AppModel model = new AppModel();

FXMLLoader firstLoader = new FXMLLoader(getClass().getResource("first.fxml"));
firstLoader.setController(new FirstController(model));
Parent firstUI = firstLoader.load();

FXMLLoader secondLoader = new FXMLLoader(getClass().getResource("second.fxml"));
secondLoader.setController(new SecondController(model));
Parent secondUI = secondLoader.load();

如果您希望保留 <fx:controller> FXML 文件中的属性,您可以使用 controllerFactory相反,它实际上指示 FXMLLoader关于如何创建 Controller :

AppModel model = new AppModel();

Callback<Class<?>, Object> controllerFactory = type -> {
    if (type == FirstController.class) {
        return new FirstController(model);
    } else if (type == SecondController.class) {
        return new SecondController(model);
    } else {
        try {
            return type.newInstance() ; // default behavior - invoke no-arg construtor
        } catch (Exception exc) {
            System.err.println("Could not create controller for "+type.getName());
            throw new RuntimeException(exc);
        }
    }
};

FXMLLoader firstLoader = new FXMLLoader(getClass().getResource("first.fxml"));
firstLoader.setControllerFactory(controllerFactory);
Parent firstUI = firstLoader.load();

FXMLLoader secondLoader = new FXMLLoader(getClass().getResource("second.fxml"));
secondLoader.setControllerFactory(controllerFactory);
Parent secondUI = secondLoader.load();

您可以通过使用(更多)反射使 Controller 工厂更加灵活;基本上您可以实现逻辑“如果 Controller 类型有一个采用 AppModel 的构造函数,则调用该构造函数,否则调用无参数构造函数”。

如果您正在创建需要执行大量此类操作的大型应用程序,那么您可以考虑使用 afterburner.fx ,这是一个实质上允许您使用注释将模型注入(inject) Controller 的框架。

关于Javafx 将参数和值从一个 Controller 传递到另一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28946651/

相关文章:

java - 使用 JavaFX 的 TableView 中没有整数值 0

asp.net-mvc - ASP.NET MVC - 使用 IEnumerable 模型插入或更新 View

c# - 从 Controller 中获取 Controller 和 Action 名称?

jquery - 从外部 Javascript 文件访问 ASP.NET MVC 模型数据

php - OpenCart 将变量从 Controller 传递给 Twig

php - 路由到子文件夹中的 Controller 在 Laravel 4 中不起作用

java - RichTextFX 的撤消功能无法正常工作

javafx - 向 JavaFX 上下文菜单添加标题

java - [JavaFx]这个绑定(bind)有什么问题?

javafx - 传递参数JavaFX FXML