Java 将fxml加载到容器中时的getParent()
这是我的主页控制器,可在Java 将fxml加载到容器中时的getParent(),java,javafx,fxml,Java,Javafx,Fxml,这是我的主页控制器,可在borderPanecenter中加载home.fxml: MainController.java public class MainController implements Initializable { @FXML private BorderPane borderPane; @Override public void initialize(URL location, ResourceBundle resources) { try {
borderPane
center中加载home.fxml
:
MainController.java
public class MainController implements Initializable {
@FXML private BorderPane borderPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
Connection connection = SqlConnection.dbConnector();
//JDBC connection initial check
if (connection != null) {
newLoginStage();
connection.close();
}
goToHome();
} catch (Exception e) {
e.printStackTrace();
}
}
//ToolBar Methods
@FXML public void goToHome() throws Exception {
toolBarButtonClick(homeTB, "home.fxml");
}
@FXML private void logout() throws Exception {
Stage stage = (Stage) borderPane.getScene().getWindow();
stage.hide();
newLoginStage();
stage.show();
}
@FXML public void exit() throws Exception {
}
private void toolBarButtonClick(Button button, String fxml) throws Exception {
button.setDisable(true);
}
private void newMainContent(String fxml) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource(fxml));
borderPane.setCenter(fxmlLoader.load());
}
public class HomeController implements Initializable {
@FXML
private BorderPane borderPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
mainContent = new MainContent(borderPane);
BorderPane parentBorderPane = (BorderPane)borderPane.getParent();
parentBorderPane.setLeft(null);
}
HomeController.java
public class MainController implements Initializable {
@FXML private BorderPane borderPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
Connection connection = SqlConnection.dbConnector();
//JDBC connection initial check
if (connection != null) {
newLoginStage();
connection.close();
}
goToHome();
} catch (Exception e) {
e.printStackTrace();
}
}
//ToolBar Methods
@FXML public void goToHome() throws Exception {
toolBarButtonClick(homeTB, "home.fxml");
}
@FXML private void logout() throws Exception {
Stage stage = (Stage) borderPane.getScene().getWindow();
stage.hide();
newLoginStage();
stage.show();
}
@FXML public void exit() throws Exception {
}
private void toolBarButtonClick(Button button, String fxml) throws Exception {
button.setDisable(true);
}
private void newMainContent(String fxml) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource(fxml));
borderPane.setCenter(fxmlLoader.load());
}
public class HomeController implements Initializable {
@FXML
private BorderPane borderPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
mainContent = new MainContent(borderPane);
BorderPane parentBorderPane = (BorderPane)borderPane.getParent();
parentBorderPane.setLeft(null);
}
我本来打算在主页上隐藏
borderPane
的左侧部分,但是我无法在HomeController.java
中使用borderPane.getParent()
获取父级,您不能只设置borderPane.setLeft(null)吗加载home.fxml
后,在MainController中的newMainContent(String fxml)
方法中的code>?在``fxmloader.load()`过程中调用控制器中的initialize()
方法,即在退出load()
之前调用该方法。因此,FXML的根节点不可能在此时添加到父节点,因此getParent()
将返回null。正如@MBec所建议的,更新包含其UI的任何节点的内容实际上不是“子”控制器的责任;这应该由主控制器完成。