JavaFX控制器注入不起作用 [英] JavaFX controller injection does not work
问题描述
我有两个 fxml
个文件。我用 include
语句连接它们:
I have two fxml
files. I connect them with an include
statement:
main fxml
文件看起来像这样:
The "main" fxml
file looks like that:
<?import javafx.geometry.*?>
// ...
<BorderPane prefHeight="962" prefWidth="1280" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MyMainController">
<center>
<SplitPane dividerPositions="0.63" BorderPane.alignment="CENTER">
<items>
<fx:include source="AnotherFile.fxml" />
// ...
</items>
</SplitPane>
</center>
<top>
// ...
</top>
</BorderPane>
第二个(=AnotherFile.fxml)就像这样:
And the second one (= "AnotherFile.fxml") like that:
<?import java.lang.*?>
// ...
<SplitPane dividerPositions="0.15" orientation="VERTICAL" prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<items>
// ...
<Label fx:id="oneOfMyLabels" text="myText" GridPane.columnIndex="2" GridPane.rowIndex="1" />
</items>
</SplitPane>
现在,我在main控制器应用程序中使用注入。 MyMainController
:
Now, I am using injections in the "main"-controller application.MyMainController
:
@FXML
private Label oneOfMyLabels;
如果我运行控制器,我会得到 java.lang.NullPointerException
异常,分别是 java.lang.reflect.InvocationTargetException
一个。在调试模式中,我发现,注入的标签
是 null
!
If I run the controller I get a java.lang.NullPointerException
exception, respectively a java.lang.reflect.InvocationTargetException
one. In debugging mode I found out, that the injected Label
is null
!
现在,我的问题:
无法从main fxml文件到达包含fxml文件的组件的 MyMainController
?我是否必须在每个fxml文件上使用自己的控制器,如果包含或不包含?!
Now, my question:
Can't reach the MyMainController
from the "main fxml file" the components of the included fxml file?? Do I have to use an own controller on each fxml file, if it is included or not?!
感谢您的帮助!!
推荐答案
您需要为每个FXML文件使用不同的控制器,并且 fx:id
-annotated elements每个文件的注入将被注入对应的控制器实例。
You need to have a different controller for each FXML file, and the fx:id
-annotated elements of each file will be injected into the corresponding controller instance.
当你包含FXML文件时,你可以为包含的文件注入控制器通过在 fx:include
元素上设置 fx:id
属性进入包含文件的控制器:
When you have included FXML files, you can inject the controller for the included file into the controller for the including file, by setting an fx:id
attribute on the fx:include
element:
mainfxml文件:
"main" fxml file:
<?import javafx.geometry.*?>
// ...
<BorderPane prefHeight="962" prefWidth="1280" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MyMainController">
<center>
<SplitPane dividerPositions="0.63" BorderPane.alignment="CENTER">
<items>
<fx:include fx:id="another" source="AnotherFile.fxml" />
// ...
</items>
</SplitPane>
</center>
<top>
// ...
</top>
</BorderPane>
和主控制器:
public class MyMainController {
@FXML
private AnotherController anotherController ;
// ...
}
(规则因为字段名称是 fx:id
属性的值,附加了Controller
。这里 AnotherController
是 AnotherFile.fxml
的控制器类。
(the rule being that the field name is the value of the fx:id
attribute with "Controller"
appended). Here AnotherController
is the controller class for AnotherFile.fxml
.
现在,您可以在包含的控制器中公开您需要访问的数据:
Now you can, for example, expose the data you need to access in the "included controller":
public class AnotherController {
@FXML
private Label oneOfMyLabels ;
public StringProperty textProperty() {
return oneOfMyLabels.textProperty();
}
public final String getText() {
return textProperty().get();
}
public final setText(String text) {
textProperty().set(text);
}
// ...
}
然后您的主控制器可以执行以下操作:
and then your main controller can do things like
anotherController.setText(...);
当然会更新标签。这样可以保留封装,因此如果您选择使用其他控件而不是标签,则这些更改不必在直接控制器之外传播。
which will of course update the label. This preserves encapsulation, so that if you choose to use another control instead of a label, those changes do not have to propagate outside of the immediate controller.
这篇关于JavaFX控制器注入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!