Javafx 将参数和值从一个控制器传递到另一个控制器
Javafx pass parameter and values from one controller to another
我是 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 传递的文本的 optimal/best 方法是什么。我的意思是,我在 txtEnter
中输入一个文本,然后按下按钮 btnSend
,在按下按钮后,我希望文本显示在 txtView
中,它正在使用另一个控制器。
我已经阅读了很多关于 observers pattern
和 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);
}
}
让您的控制器可以访问模型:
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
class 中执行
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();
您可以通过使用(更多)反射使控制器工厂更加灵活;基本上你可以实现逻辑 "if the controller type has a constructor taking an AppModel
, call that constructor, otherwise call the no-arg constructor".
如果您正在创建一个需要执行大量此类操作的大型应用程序,那么您可以考虑使用 afterburner.fx,这是一个框架,它本质上允许您使用注释将模型注入控制器。
我是 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 传递的文本的 optimal/best 方法是什么。我的意思是,我在 txtEnter
中输入一个文本,然后按下按钮 btnSend
,在按下按钮后,我希望文本显示在 txtView
中,它正在使用另一个控制器。
我已经阅读了很多关于 observers pattern
和 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);
}
}
让您的控制器可以访问模型:
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
class 中执行
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();
您可以通过使用(更多)反射使控制器工厂更加灵活;基本上你可以实现逻辑 "if the controller type has a constructor taking an AppModel
, call that constructor, otherwise call the no-arg constructor".
如果您正在创建一个需要执行大量此类操作的大型应用程序,那么您可以考虑使用 afterburner.fx,这是一个框架,它本质上允许您使用注释将模型注入控制器。