簡體   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