[英]javafx set parent borderpane to null
i have 2 fxml 我有2 fxml
FXML A: it contains borderpane with ID fx:id="UnitBorderPane" FXML答:它包含ID为fx:id =“ UnitBorderPane”的borderpane
FXML B: it contains anchorpane with ID fx:id="UnitForm" FXML B:它包含ID为fx:id =“ UnitForm”的锚定窗格
i load the "FXML B" at borderpane A on the left side 我在左侧边框A上加载了“ FXML B”
FXMLLoader loader = new
FXMLLoader(getClass().getResource("/projectname/unit/UnitForm.fxml"));
Pane pane = (Pane) loader.load();
UnitBorderPane.setLeft(pane);
it is kind of fxml form, so we have a button with action 这是一种fxml形式,所以我们有一个带有操作的按钮
<Button layoutX="102.0" layoutY="169.0" mnemonicParsing="false" onAction="#saveUnit" text="Save" />
how to hide that FXML A BorderPane left? 如何隐藏一个BorderPane留下的FXML?
@FXML
private void saveUnit(ActionEvent event) {
BorderPane borpane = (BorderPane)UnitForm.getParent().lookup("#UnitBorderPane");
borpane.setLeft(null);
}
this code not work, the borpane variable is null so i cannot set the borderPane FXML A Left to null. 此代码无法正常工作,因此borpane变量为null,因此我无法将borderPane FXML A Left设置为null。
I think it should just be 我认为应该是
BorderPane borpane = (BorderPane)UnitForm.getParent();
However, none of this feels very robust; 但是,这些都不是很健壮。 for example if you decide to change the layout structure at all you will likely have a lot of code to change in various classes.
例如,如果您决定完全更改布局结构,则可能需要在各个类中更改很多代码。 I would add a property to the controller for
UnitForm.fxml
that you can observe from the controller for UnitBorderPane
. 我会为
UnitForm.fxml
的控制器添加一个属性,您可以从UnitBorderPane
的控制器中观察到。 Something like: 就像是:
public class UnitFormController { // your actual class name may differ....
private final BooleanProperty saved = new SimpleBooleanProperty();
public BooleanProperty savedProperty() {
return saved ;
}
public final boolean isSaved() {
return savedProperty().get();
}
public final void setSaved(boolean saved) {
savedProperty().set(saved);
}
// other code as you already have...
@FXML
private void saveUnit() {
setSaved(true);
}
// ...
}
Then you do 那你做
FXMLLoader loader =
new FXMLLoader(getClass().getResource("/projectname/unit/UnitForm.fxml"));
Pane pane = (Pane) loader.load();
UnitFormController controller = loader.getController();
controller.savedProperty().addListener((obs, wasSaved, isNowSaved) -> {
if (isNowSaved) {
UnitBorderPane.setLeft(null);
}
});
UnitBorderPane.setLeft(pane);
Now the management of the UnitBorderPane
is all in one place, instead of being split over two controllers, and there are no lookups (which are not robust). 现在,
UnitBorderPane
的管理全部集中在一个地方,而不是被两个控制器分割,而且没有查找(不可靠)。 The controller for the UnitForm
just sets a property and lets the other controller respond as it sees fit. UnitForm
的控制器仅设置一个属性,并让另一个控制器根据需要进行响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.