简体   繁体   English

JavaFX Controller类变量未绑定到其FXML对等变量

[英]JavaFX Controller class variables not binding to their FXML counterparts

When opening a new javafx window from a running javafx application I cannot bind the fxml variables to a local variable in the controller class. 从正在运行的javafx应用程序打开新的javafx窗口时,我无法将fxml变量绑定到控制器类中的局部变量。

Please note that for the running application I am able to bind to like-named variables without a problem, populating ComboBoxes in the running application at runtime. 请注意,对于正在运行的应用程序,我可以毫无问题地绑定到同名变量,可以在运行时在正在运行的应用程序中填充ComboBoxes。 Any solutions are welcome. 欢迎任何解决方案。

Code that calls the new class (ServerConfigChooser) 调用新类的代码(ServerConfigChooser)

FXMLLoader loader = new FXMLLoader(getClass().getResource("ServerConfigChooser.fxml"));
try{
    Stage stage = new Stage();
    stage.setScene(new Scene( (Parent) loader.load()));
    stage.show();
} catch (IOException ex)...

Example of binding that works in the running application (code executed at runtime) 在正在运行的应用程序中起作用的绑定示例(在运行时执行的代码)

@FXML
public ComboBox cb_01_fxid;

private void initComboBox(){
    cb_01_fxid.getItems().add(0, "yes");
    cb_01_fxid.getItems().add(0, "no");
}

the fxid "cb_01_fxid" is identical in the controller class to the fxid of the ComboBox object in the .fxml file. 控制器类中的fxid“ cb_01_fxid”与.fxml文件中ComboBox对象的fxid相同。 This binds without a problem. 这绑定没有问题。 Below is the code from the controller class for the new window (ServerConfigChooser). 以下是新窗口(ServerConfigChooser)的控制器类中的代码。

1 @FXML
2 public ComboBox cb_02_fxid;
3
4 public void initComboBoxNewWindow(){
5     cb_02_fxid.addItems(0, "test item 1");
6 }

and the relevant fxml lines from the main application 以及来自主应用程序的相关fxml行

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="728.9999000000025" prefWidth="735.0000999999975" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="model.Sample">

and the new window 和新窗口

<fx:root type="javafx.scene.layout.AnchorPane" id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="283.0" prefWidth="445.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="model.ServerConfigChooser">
<ComboBox id="cb_02_fxid" layoutX="256.0" layoutY="84.0" onAction="#scc_cb_action">

the program throws a null pointer exception on line 5 (line numbers added for reference). 程序在第5行(添加行号以供参考)上引发空指针异常。 Does anyone know why the second controller isn't binding to the second fxml object? 有谁知道为什么第二个控制器没有绑定到第二个fxml对象? Thanks in advance 提前致谢

Your ComboBox fxml part should have fx:id attribute set: 您的ComboBox fxml部分应设置fx:id属性:

<ComboBox fx:id="cb_02"

This id should have EXACTLY same name as your variable in Controller class. 此ID应与Controller类中的变量名称完全相同。

See tutorial for details: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm 有关详细信息,请参见教程: http : //docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM