JAVAFX-控制器之间共享对象 [英] JAVAFX - share object between controllers
问题描述
请如何在控制器之间共享对象用户?我有一个TabPanelController。在初始化时,将带有对象User的数据发送到其他控制器。可以,但是在显示阶段之前我无法访问该对象。
Please how to share object User between Controllers? I have a TabPanelController. In initialization send to other controllers Data with object User. Its OK, but I cant acces to this object before stage shown.
在控制器在Stage之前加载控制器时,是否可以在Controller的initialize方法中访问Stage?或者如何在加载程序中处理onWindowShow(当显示Windows时,对象User已填充)?
Its possible acces to Stage in initialize method in controller, when controller is loaded before Stage? Or how handle onWindowShow in loader (When windows show, object User was filled)?
@FXML
private ActionController panelActionController;
@FXML
private StoreController panelStoreController;
@FXML
private ProfilesController panelProfilesController;
@FXML
private UsersController panelUsersController;
@FXML
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Platform.runLater(new Runnable() {
@Override
public void run() {
menuUserInfo.setText(" " + loggedInUser.getName() + " " +
loggedInUser.getForname() + " (" + loggedInUser.getLogin() + ")");
menuUserInfo.setUserData(loggedInUser);
panelActionController.setLoggedUser(loggedInUser);
panelStoreController.setLoggedUser(loggedInUser);
panelProfilesController.setLoggedUser(loggedInUser);
panelUsersController.setLoggedUser(loggedInUser);
}
});
}
在panelActionController中
In panelActionController
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println(loggedInUser); // NULL..
}
当我在舞台上按下任意按钮时,loggedUser不会空值。但是我需要在初始化期间按用户角色(管理员,用户)设置按钮,文本字段。
When i press any button in stage, loggedUser is not null. But i need set buttons, textfields by user role (Admin, User) during initialization.
推荐答案
已解决:
1。 fxml中的主TabPanel控制器中的注入控制器
必须具有带有fx:id = panelAction的根元素(例如GridPane)。
控制器必须命名:FX:GridPane的ID + word控制器: panelActionController
in fxml must have a root element (e.g. GridPane) with fx:id="panelAction". Controller must name: fx:id of GridPane + word Controller : panelActionController
@FXML
private ActionController panelActionController;
@FXML
private StoreController panelStoreController;
@FXML
private ProfilesController panelProfilesController;
@FXML
private UsersController panelUsersController;
2。我在主TabPanelController中为所有子控制器编写了吸气剂:
public ActionController getActionController() {
return panelActionController;
}
public StoreController getStoreController() {
return panelStoreController;
}
public ProfilesController getProfilesController() {
return panelProfilesController;
}
public UsersController getUsersController() {
return panelUsersController;
}
3。并在主TabPanelStage中(这里有Object loginInUser)获取所有控制器
TabPanelController tabPanelController = fxmlLoader.getController();
ActionController actionController = tabPanelController.getActionController();
StoreController storeController = tabPanelController.getStoreController();
ProfilesController profilesController = tabPanelController.getProfilesController();
UsersController usersController = tabPanelController.getUsersController();
4。然后:
actionController.setComponentsByRole(loggedInUser.getRole());
这篇关于JAVAFX-控制器之间共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!