如何FXML控制器1创建的对象传递给内FXML控制
的控制器2我的JavaFX 2.0应用程序,它由两个FXML文件,和2个控制器,用于他们+一个“主” java文件。如何FXML控制器1创建的对象传递给内FXML控制
的开始时间,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按钮时,在控制器1类的事件处理程序,我改变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);
}
而且,在那之后,我必须做一些工作,对下一场景的对象,而且它必须是不一样的类的新实例,但对象,我已经在第一个场景中初始化了。
我知道如何让这些全部采用“非标准的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 />
我使用1.7.0_21, 它现在这样的代码: 在主应用程序FXML文件,
<VBox ...>
<fx:include fx:id="tom" source="Tom.fxml" />
</VBox>
和包含的fxml可以定义它自己的fxml文件,如下所示:
<AnchorPane id="AnchorPane" fx:id="tomPan" ... xmlns:fx="http://javafx.com/fxml" fx:controller="**com.tom.fx.TomController**">
,然后,在主应用程序位指示所需要的“Tom.fxml”的控制器是这样的:
@FXML private TomController tomController;
通知‘@FXML’。也许它会自动调用控制器。
感谢您的贡献!一个问题:fx:id是tom,但注入的控制器名为tomController?他们通过什么机制相关联? – 2014-01-23 15:50:52
JavaFX的注入“控制器”,以外汇:身份证。 – Marckaraujo 2016-12-13 16:59:00
如果现在还不清楚,JavaFX的将自动注入控制器到名为
什么root.cb. **分钟** .getItems()。add(“new item”);和** cc ** .setRoot(root);在最后的代码片? – 2012-05-23 11:37:03
对不起,'分钟'来自错误的复制粘贴。和来自维多利亚州原始代码样本的'cc'。更新。 – 2012-05-23 12:35:08