繁体   English   中英

在 java fxml 中将子元素添加到 fx:root 元素的子元素

[英]Add children to child of fx:root element in java fxml

我有一个问题,因为我知道如何将新组件添加到 fx:root 容器子级。

这就是我目前所拥有的。

名为 PopupContainer 的根元素

<fx:root type="StackPane" alignment="CENTER" xmlns:fx="http://javafx.com/fxml"
     styleClass="popup">
    <VBox alignment="CENTER">
        <HBox alignment="CENTER">
            <VBox fx:id="content" alignment="CENTER" spacing="5" styleClass="whiteBackground, blackborder"
              fillWidth="false" StackPane.alignment="CENTER">
            <!-- this is where I would like to add components -->
            </VBox>
        </HBox>
    </VBox>
</fx:root>

我也有 controller 。

现在,我想在其他一些 fxml 中像这样使用它:

<PopupContainer xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="com.example.bank.editbank.EditBankPresenter"
            styleClass="popup"
            fx:id="container">
    <!-- those components should go to VBOX up there -->
    <ViewTitle label="%editBankUC"/>
    <Button fx:id="someButton" text="Click me"/>
</PopupContainer>

当然,当我添加组件时,它们 go 直接位于 StackPane 下,因为它是布局的根目录。 我试图覆盖 getChildren() 方法以返回 VBox 子级,但我检测到了子级循环。 我不想以编程方式添加它们,因为在应用程序中有超过 300 个这样的案例,但我可以添加新标签(而不是例如其他东西)。 谢谢!

回答我自己的问题,因为我认为其他人也想知道这一点。

所以,正如我之前已经拥有的,这是 PopupContainer.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.geometry.Insets?>
<fx:root type="StackPane" alignment="CENTER" xmlns:fx="http://javafx.com/fxml"
         styleClass="popup">
    <VBox fx:id="child1" alignment="CENTER">
        <HBox alignment="CENTER">
            <VBox fx:id="content" alignment="CENTER" spacing="5" styleClass="whiteBackground, blackborder"
                  fillWidth="false" StackPane.alignment="CENTER">

                <padding>
                    <Insets topRightBottomLeft="10.0" />
                </padding>
            </VBox>
        </HBox>
    </VBox>
</fx:root>

和 controller PopupContainer.java:

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

import java.io.IOException;

public class PopupContainer extends StackPane {

    //refference to VBox from layout
    @FXML private VBox content;

    public PopupContainer() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("PopupContainer.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    //note this getter, this is the key that allow you to add childred to child of this component
    public ObservableList<Node> getItems() {
        return content.getChildren();
    }
}

最后的用法是这样的:

<PopupContainer xmlns="http://javafx.com/javafx"
                xmlns:fx="http://javafx.com/fxml"
                fx:controller="com.example.bank.editbank.EditBankPresenter"
                styleClass="popup"
                fx:id="container"
>
    <!-- this is what was acceptable to do in question so instead of children I am using items (setter in PopupContainer.java) -->
    <items>
        <ViewTitle label="%editBankUC"/>
        <HBox VBox.vgrow="ALWAYS">
            <Pane minWidth="20"/>
            <VBox alignment="CENTER" spacing="5">
                <HorizontalTextInput fx:id="name" label="%nameUC" alignment="CENTER_RIGHT" />
                <HorizontalTextInput fx:id="bic" label="%bicUC" alignment="CENTER_RIGHT" />
                <AddressInput fx:id="address" />
                <HorizontalCheckboxInput fx:id="active" label="%activeUC" />
            </VBox>
            <Pane minWidth="20"/>
        </HBox>
        <HBox alignment="CENTER" spacing="5">
            <JFXButton fx:id="close" onAction="#closeView" text="%closeUC" />
            <JFXButton fx:id="edit" onAction="#editClicked" />
            <padding>
                <Insets top="10.0" bottom="10.0" />
            </padding>
        </HBox>
    </items>
</PopupContainer>

我希望很清楚,如何添加它。 我在其他地方没有发现任何熟悉的东西,但是查看 BorderPane 的来源可以给你一个提示如何做到这一点。 干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM