如何将在FXML Controller1中创建的对象传递给内部FXML控件的Controller2 [英] How to pass object created in FXML Controller1 to Controller2 of inner FXML control
问题描述
我有JavaFX 2.0应用程序,该应用程序由两个FXML文件和两个用于它们的控制器+一个主" .java文件组成.
在开始时,像这样初始化FXML1:
public void start(Stage stage) throws Exception {
stage.setTitle("Demo Jabber JavaFX Chat");
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"),
ResourceBundle.getBundle("fxmlexample.fxml_example"));
Scene scene = new Scene(root, 226, 264);
stage.setScene(scene);
scene.getStylesheets().add("fxmlexample/fxmlstylesheet.css");
stage.show();
}
然后,当单击来自scene1的按钮时,在Controller1类的事件处理程序中,我更改scene1的根目录,以为用户显示新的GUI视图.然后在此控制器中初始化一些对象.例如这样的
public class FXMLExampleController {
//some fields...
private MySuperObject c;
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
//some fields...
c = new MySuperObject(); //here i initialize my object, i'm interested in
try {
c.login(username, password); // some actions with this object, which i need to make.
Scene cc = buttonStatusText.getScene();
Parent root = null;
try {
//changing a scene content...
root = FXMLLoader.load(getClass().getResource("fxml_example2.fxml"),
ResourceBundle.getBundle("fxmlexample.fxml_example"));
} catch (IOException ex) {
Logger.getLogger(FXMLExampleController.class.getName()).log(Level.SEVERE, null, ex);
}
cc.setRoot(root);
}
然后,在那之后,我必须在下一个场景中对该对象做一些工作,并且它一定不是同一个类的新实例,而是我在第一个场景中初始化的对象. /p>
我了解如何使用标准Java"来制作所有这些内容,但是我对使用JavaFX + FXML的这项任务感到困惑.
在FX 2.2中,引入了用于控制器节点的新API:
// create class which is both controller and node
public class InnerFxmlControl extends HBox implements Initializable {
@FXML public ComboBox cb;
public InnerFxmlControl () {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml_example2.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
带有下一个fxml(注意标记fx:root
):
<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml">
<children>
<ComboBox fx:id="cb" />
</children>
</fx:root>
到此,您已经创建了一个新控件,可以将其用作常规JavaFX控件.例如.在您的情况下:
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
// you just create new control, all fxml tricks are encapsulated
InnerFxmlControl root = new InnerFxmlControl();
// and you can access all its' methods and fields including matched by @FXML tag:
root.cb.getItems().add("new item");
Scene cc = buttonStatusText.getScene();
cc.setRoot(root);
}
和在fxml中:
<InnerFxmlControl />
I have JavaFX 2.0 application, which consists of two FXML files, and 2 Controllers for them + one "main" .java file.
At the start time, FXML1 is initialized, like this:
public void start(Stage stage) throws Exception {
stage.setTitle("Demo Jabber JavaFX Chat");
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"),
ResourceBundle.getBundle("fxmlexample.fxml_example"));
Scene scene = new Scene(root, 226, 264);
stage.setScene(scene);
scene.getStylesheets().add("fxmlexample/fxmlstylesheet.css");
stage.show();
}
Then, when a button from scene1 is clicked, in its event handler in Controller1 class, I change scene1 root, to show new gui-view for a user. And in this controller I initialize some object. For example like this:
public class FXMLExampleController {
//some fields...
private MySuperObject c;
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
//some fields...
c = new MySuperObject(); //here i initialize my object, i'm interested in
try {
c.login(username, password); // some actions with this object, which i need to make.
Scene cc = buttonStatusText.getScene();
Parent root = null;
try {
//changing a scene content...
root = FXMLLoader.load(getClass().getResource("fxml_example2.fxml"),
ResourceBundle.getBundle("fxmlexample.fxml_example"));
} catch (IOException ex) {
Logger.getLogger(FXMLExampleController.class.getName()).log(Level.SEVERE, null, ex);
}
cc.setRoot(root);
}
And, after that, i have to do some work with that object on the next scene, and it must be NOT a new instance of the same class, but the object, i've initialized on the first one scene.
I understand how to make these all using "standart java", but i'm kind of confused on this task using JavaFX + FXML.
In FX 2.2 new API for controller-node was introduced:
// create class which is both controller and node
public class InnerFxmlControl extends HBox implements Initializable {
@FXML public ComboBox cb;
public InnerFxmlControl () {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml_example2.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
with next fxml (note tag fx:root
):
<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml">
<children>
<ComboBox fx:id="cb" />
</children>
</fx:root>
By this you've created a new control, which you can use as regular JavaFX controls. E.g. in your case:
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
// you just create new control, all fxml tricks are encapsulated
InnerFxmlControl root = new InnerFxmlControl();
// and you can access all its' methods and fields including matched by @FXML tag:
root.cb.getItems().add("new item");
Scene cc = buttonStatusText.getScene();
cc.setRoot(root);
}
and in fxml:
<InnerFxmlControl />
这篇关于如何将在FXML Controller1中创建的对象传递给内部FXML控件的Controller2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!