我的根布局包含2种布局:-OptionsPane -DrawArea
I have root layout containing 2 layouts: - OptionsPane - DrawArea
我正在尝试在OptionsPaneController中访问DrawAreaController进行调用它的绘制方法。下面是OptionsPaneController中的初始化方法:
What i am trying is to access DrawAreaController in OptionsPaneController to call its draw method. Below is initialize method from OptionsPaneController:
@Override public void initialize(URL url, ResourceBundle rb) { newDragonButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if(newDragonButton.getText().equals("Nowy")){ //do something } else{ newDragonButton.setText("Nowy"); FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DrawArea.fxml")); DrawAreaController dac = (DrawAreaController) loader.getController(); Dragon dragon = new Dragon(600, 300, 20, 2, 90, 270, colorChooser.getValue()); if(dac == null) System.out.println("controller = null"); dac.drawDragon(dragon); } } }); }不幸的是dac始终为空。
Unfortunately dac is always null.
我的代码中是否存在任何错误,或者是否不可能在另一个控制器中加载控制器?
Is there any mistake in my code or is it impossible to load controller in another controller?
推荐答案该控制器始终为null,因为您从不加载FXMLLoader。
The controller is always null, because you never load the FXMLLoader.
load() 加载fxml并为您实例化控制器实例。如果在 getController()之前不使用此方法,它将始终返回 null
The load() loads the fxml and instantiates the controller instance for you. If you do not use this method before getController(), it will always return null
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DrawArea.fxml")); Parent root = loader.load(); DrawAreaController dac = (DrawAreaController) loader.getController();您可能会或可能不想存储FXML的根。
更多推荐
另一个控制器类中的访问控制器
发布评论