简体   繁体   English

如何在JavaFX中交换父节点的子节点?

[英]How do I swap the children of a parent node in JavaFX?

I want to swap the Button with a TextField. 我想将Button与TextField交换。 How to do it? 怎么做?

I heard there are methods toFront () and toBack (). 我听说有toFront()和toBack()方法。 But then children nodes must be wrapped in a Group. 但是子节点必须包装在一个组中。 Is that the truth? 那是事实吗?

Is there another way? 还有另一种方法吗?

在此处输入图片说明

<ToolBar fx:id="toolbarForActivityPanel" layoutX="22.0" layoutY="316.0" prefHeight="0.0" prefWidth="518.0" style="-fx-background-color: black;" AnchorPane.bottomAnchor="21.0" AnchorPane.leftAnchor="21.0" AnchorPane.rightAnchor="21.0">
                      <items>
                        <Button fx:id="createOfActivityButton" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="46.0" prefWidth="48.0" style="-fx-background-color: black; -fx-border-color: white; -fx-border-insets: 0px 10px 0px 0px;">
                             <graphic>
                                <ImageView fitHeight="30.0" fitWidth="32.0" pickOnBounds="true" preserveRatio="true" style="-fx-background-color: black; -fx-border-radius: 4px;">
                                   <image>
                                      <Image url="@../../img/icons_of_activities/icons8-plus-math-48.png" />
                                   </image>
                                </ImageView>
                             </graphic>
                             <cursor>
                                <Cursor fx:constant="HAND" />
                             </cursor>
                             <opaqueInsets>
                                <Insets />
                             </opaqueInsets>
                          </Button>
                          <Button mnemonicParsing="false" prefHeight="46.0" prefWidth="48.0" style="-fx-background-color: black; -fx-border-color: white; -fx-border-insets: 0 10 0 0;">
                             <cursor>
                                <Cursor fx:constant="HAND" />
                             </cursor>
                             <graphic>
                                <ImageView fitHeight="30.0" fitWidth="32.0" pickOnBounds="true" preserveRatio="true" style="-fx-background-color: black; -fx-border-radius: 4px;">
                                   <image>
                                      <Image url="@../../img/icons_of_activities/icons8-search-filled-50.png" />
                                   </image>
                                </ImageView>
                             </graphic>
                          </Button>
                       <TextField />
                      </items>
                    </ToolBar>

The items in a toolbar appear visually in the order in which they appear in the items list. 工具栏中的项目按照它们在items列表中出现的顺序直观地显示。 So to re-order them you simply need to reorder the items in the list. 因此,要重新排序它们,您只需要重新排序列表中的项目即可。

So you can do something like 所以你可以做类似的事情

// remove the button:
toolbarForActivityPanel.getItems().remove(createOfActivityButton);
// remove the text field (need to provide an fx:id for the text field in FXML):
toolbarForActivityPanel.getItems().remove(textField);

// add the text field as the first element:
toolbarForActivityPanel.getItems().add(0, textField);
// add the button as the third element:
toolbarForActivityPanel.getItems().add(2, createOfActivityButton);

You can manipulate the items list in any arbitrary way like this (add new elements, remove any elements, etc). 您可以通过这种任意方式操作项目列表(添加新元素,删除任何元素等)。 The only caveat is to ensure that you never have the same item twice in the list. 唯一的警告是要确保您在列表中永远不会两次出现相同的项目。

Normally javafx when loading form an FXML-file loads and displays them in the order that they are in the file so in your case you could simply switch the <TextField /> and the first button in FXML-file. 通常,在从FXML文件加载时,javafx会按照它们在文件中的顺序加载并显示它们,因此,在您的情况下,您只需切换<TextField />和FXML文件中的第一个按钮即可。

This is not the case in some panes, such as gridpanes where you can specify the row and column in the grid of the element in the element 在某些窗格(例如gridpanes)中不是这种情况,您可以在其中指定元素中元素的网格中的行和列

<GridPane>
    <Text text="Welcome" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2"/>

    <Label text="User Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/>

    <TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/>

    <Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2"/>

    <PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
</GridPane>

So you could either swap the order around or switch to a differnt pane and explicitly layout its children 因此,您既可以交换订单,也可以切换到其他窗格并显式布局其子级

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

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