本文介绍了如何通过单击“添加"自动在GridPane(Javafx)中添加“上面的行".按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在GridPane的上方添加一个索引为0的行,问题是当我在GridPane的 MyGrid.add(Label,0,0)中添加一个元素时,此标签将与位置中的前一个标签(0,0)

I would like to add a Row above in GridPane which index 0, the problem is when I add an element in GridPane MyGrid.add(Label, 0, 0) this label will overlap with the previous Label in emplacement (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);

推荐答案

您必须增加 GridPane 中先前每个孩子的行索引,即类似这样的东西:

You have to increment the row index of every child previously in the GridPane, i.e. something like this:

//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 s存储在字段中,而不在按钮的操作处理程序中创建它们,这似乎是错误的,因为您不能在场景中多次使用 Node .

Note: If you want to add the row multiple times storing the new Nodes in fields and not creating them in the action handler of the button seems wrong, since you can't use the Nodes in the scene more than once.

这篇关于如何通过单击“添加"自动在GridPane(Javafx)中添加“上面的行".按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:07