@FXML对象在initialize方法中为null [英] @FXML Objects are null in initialize Method
问题描述
我有一个(缩短的)java FX类:
i have a (shortened) java FX Class:
public class MyReportController extends
javafx.application.Application implements Initializable {
@FXML
private CustomTextField autoTextField;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
TextFields.bindAutoCompletion(autoTextField, ServiceLocator
.getInstance().getMeasurementService()
.getAllMeasurementNumbers());
}......
initialzie方法被调用,但是autoTextField(和所有其他FXML组件)为空.
the initialzie method is called, but the autoTextField(and all other FXML components) is null.
应用程序是使用main方法从另一个类启动的.首先,我在控制器类中拥有main方法,其结果是,该控制器类被实例化了两次.我认为我当前的问题与此有关.
The Application is started from another class with the main method. First i had the main Method in the controller class with the result, that the controller class was instantiated twice. I think my current problem is related to that.
推荐答案
您必须在FXML中具有与要插入的属性名称相同的 fx:id
声明.因此,例如,在FXML中,CustomField控件声明应具有 fx:id = autoTextField
.这样,依赖项将在初始化时注入.
You have to have the same fx:id
declarations in your FXML as property names to inject. So for example, in FXML, CustomField controll declaration should have fx:id=autoTextField
. This way dependencies will be injnected upon initialization.
第二件事,请不要使用扩展 Application
的控制器类.这根本没有目的.从与控制器不同的类启动应用程序,并且 FXMLLoader
Second thing, please dont use controller class that extends Application
. This has simply no purpose here. Start application from different class that controller, and the controller class instance will be automaticly created once by FXMLLoader
这篇关于@FXML对象在initialize方法中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!