【问题标题】:Is there a way to insert children at specific position?有没有办法在特定位置插入孩子?
【发布时间】:2016-06-18 08:34:19
【问题描述】:

这是我的 fxml 的一部分:

<VBox>
  <children>
    <Button/>
    <Button/>
  </children>
</VBox>

我想在 vBox 中添加一个新的孩子。但我想将它添加到特定位置,例如“位置 2”的两个按钮之间。

【问题讨论】:

  • 您的意思是在控制器中添加它(即使用 Java 代码)?
  • 是的,比如 vbox.getChildren().add(myNewButton);
  • 但我想说“add(component, position);”之类的话
  • getChildren() 只返回一个列表。您可以使用为常规列表定义的任何方法。

标签: javafx model-view-controller javafx-8 fxml vbox


【解决方案1】:

窗格的子列表是ObservableList,它只是普通旧java.util.List 的子接口。因此,您可以访问所有常用的list methods。假设您在控制器中引用了VBox,将其命名为vbox,您可以这样做

vbox.getChildren().add(1, myNewButton);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2015-03-29
    • 1970-01-01
    相关资源
    最近更新 更多