如何通过单击“添加”按钮自动在GridPane(Javafx)中添加Row Above

时间:2016-02-26 10:54:21

标签: javafx

我想在GridPane中添加一个上面的索引0,问题是当我在GridPane MyGrid.add(Label, 0, 0)中添加一个元素时,这个标签会与前面的标签重叠(0,0)

我在FXML中的网格

<GridPane fx:id="CalenderGrid" hgap="5.0" layoutX="464.0" layoutY="225.0" prefHeight="191.0" prefWidth="271.0" vgap="5.0">
         <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
         </rowConstraints>
         <children>
            <Label text="Effectif" />
            <Label text="Femme" GridPane.rowIndex="1" />
            <Label text="Admins et managements" GridPane.rowIndex="2" />
            <Label text="Ingénieurs et techniciens" GridPane.rowIndex="3" />
            <Label text="Ouvriers et saisonniers" GridPane.rowIndex="4" />
            <TextField fx:id="NbrFemmes" prefHeight="25.0" prefWidth="100.0" promptText="nbr de Personnes" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <TextField fx:id="NbrAdmManag" prefHeight="25.0" prefWidth="100.0" promptText="nbr de Personnes" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <TextField fx:id="NbrIngTech" prefHeight="25.0" prefWidth="100.0" promptText="nbr de Personnes" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <TextField fx:id="NbrOuvrSais" prefHeight="25.0" prefWidth="100.0" promptText="nbr de Personnes" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <TextField fx:id="Effectif" prefHeight="25.0" prefWidth="100.0" promptText="ZIM" GridPane.columnIndex="1" />
         </children>
         <padding>
            <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
         </padding>
      </GridPane>
控制器中的

代码

private Label AnneeInfo = new Label("année");
private TextField AnneeEffectif = new TextField();



//Action in button
AnneeEffectif.setLayoutX(60);
CalenderGrid.add(AnneeInfo, 0, 0);
CalenderGrid.add(AnneeEffectif, 1, 0);

1 个答案:

答案 0 :(得分:0)

你必须增加GridPane之前的每个孩子的行索引,例如:

//Action in button
AnneeEffectif.setLayoutX(60);

// add constraint for new row
List<RowConstraints> constraints = CalenderGrid.getRowConstraints();
constraints.add(constraints.get(0));

// move all children down one row
insertRows(1);

CalenderGrid.addRow(0, AnneeInfo, AnneeEffectif);
private void insertRows(int count) {
    for (Node child : CalenderGrid.getChildren()) {
        Integer rowIndex = GridPane.getRowIndex(child);
        GridPane.setRowIndex(child, rowIndex == null ? count : count + rowIndex);
    }
}

注意:如果你想多次添加行在字段中存储新的Node而不在按钮的动作处理程序中创建它们似乎是错误的,因为你可以&# 39;多次使用场景中的Node