FXML控制器中FXML字段为null但事件响应正常的问题求助
问题描述
在为selection_bar.fxml编写的SelectionBarController控制器类中,发现@FXML注解标记的home_btn、patient_btn、setting_btn字段均为null,但按钮的Action事件可以正常响应。
相关代码
控制器代码
public class SelectionBarController { @FXML private Button home_btn; @FXML private Button patient_btn; @FXML private Button setting_btn; private MainView mainView; private UserModel viewModel; public void initSelectionBarController(MainView mainView, UserModel viewModel) { this.mainView = mainView; this.viewModel = viewModel; if (home_btn == null && patient_btn == null && setting_btn == null) { System.out.println("fxml field values are null"); } } public void switchHomePage(Event e) { System.out.println("switchHomePage clicked"); } public void switchPatientPage(Event e) { System.out.println("switchPatientPage clicked"); } }
调用类代码
public class MainView { private final Stage stage; private final BorderPane root; private final UserModel viewModel; private ToolBar selectionBar; private SelectionBarController selectionBarController; private PatientPageController patientPageController; private AnchorPane homePage; private BorderPane patientPage; //Size of the window private static final double WINDOW_X = 800; private static final double WINDOW_Y = 600; public MainView(Stage stage, UserModel viewModel) { this.stage = stage; this.viewModel = viewModel; root = new BorderPane(); stage.show(); initializeUserInterface(); } public void initializeUserInterface() { drawBackground(); drawToolBar(); stage.show(); } public void drawBackground() { Scene scene = new Scene(root, WINDOW_X, WINDOW_Y); stage.setScene(scene); } public void drawToolBar() { try { // linking fxml to controller FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/selection_bar.fxml")); selectionBar = loader.load(); selectionBarController = loader.<SelectionBarController>getController(); selectionBarController.initSelectionBarController(this, viewModel); } catch (IOException io) { System.out.println("selection bar not loaded"); } root.setTop(selectionBar); } }
FXML文件代码
<ToolBar prefHeight="30.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.java.clinic.controller.SelectionBarController"> <items> <Button id="home_btn" mnemonicParsing="false" style="-fx-background-color: transparent;" text="Home" onAction="#switchHomePage"> <opaqueInsets> <Insets/> </opaqueInsets> </Button> <Button id="patient_btn" mnemonicParsing="false" style="-fx-background-color: transparent;" text="Patient" onAction="#switchPatientPage"> <opaqueInsets> <Insets/> </opaqueInsets> </Button> <Button id="setting_btn" mnemonicParsing="false" style="-fx-background-color: transparent;" text="Settings" onAction="#switchSettingPage"> <opaqueInsets> <Insets/> </opaqueInsets> </Button> </items> </ToolBar>
运行结果
fxml field values are null
解决方案
问题出在FXML文件中按钮的属性使用错误:FXML中实现控制器字段注入需要使用fx:id属性,而你当前用的是id属性。id只是节点的通用标识符,和FXML注入无关;fx:id才是FXML用来和控制器中@FXML标记的字段建立绑定的属性。
修改FXML文件中每个Button的id为fx:id即可解决字段为null的问题,修改后的FXML按钮部分如下:
<Button fx:id="home_btn" mnemonicParsing="false" style="-fx-background-color: transparent;" text="Home" onAction="#switchHomePage"> <opaqueInsets> <Insets/> </opaqueInsets> </Button> <Button fx:id="patient_btn" mnemonicParsing="false" style="-fx-background-color: transparent;" text="Patient" onAction="#switchPatientPage"> <opaqueInsets> <Insets/> </opaqueInsets> </Button> <Button fx:id="setting_btn" mnemonicParsing="false" style="-fx-background-color: transparent;" text="Settings" onAction="#switchSettingPage"> <opaqueInsets> <Insets/> </opaqueInsets> </Button>
另外,事件能正常响应是因为onAction绑定的是控制器中的方法名,不需要依赖fx:id,所以不受影响。
内容的提问来源于stack exchange,提问作者shonn Li
相关产品推荐
相关产品推荐

