在模型中使用可观察的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);
}
}
让你的控制器可以访问模型:
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));
}
}
现在有点棘手的部分是控制器没有无参数构造函数,这意味着 FXMLLoader 创建它们的默认机制将不起作用。最简单的方法是手动设置它们。 从 FXML 文件中删除 <fx:controller> 属性,然后在您的 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();
如果您希望在 FXML 文件中保留 <fx:controller> 属性,则可以改用 controllerFactory,它实质上指示 FXMLLoader 如何创建控制器:
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();
您可以通过使用(更多)反射使控制器工厂更加灵活;基本上你可以实现逻辑“如果控制器类型有一个构造函数采用AppModel,则调用该构造函数,否则调用无参数构造函数”。
如果您正在创建需要执行大量此操作的大型应用程序,那么您可以考虑使用afterburner.fx,它是一个框架,本质上允许您使用注释将模型注入控制器。