2016-02-26 77 views
0

我想在GridPane上添加一行,索引0,问题是当我在GridPane中添加一个元素MyGrid.add(Label, 0, 0)这个标签会重叠以前的标签在炮位(0,0)如何在GridPane(Javafx)中自动添加Row Above通过点击“添加”按钮

我在网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); 

回答

0

你必须增加每通道的行索引ILD以前在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秒且不在操作创建它们按钮的处理程序似乎是错误的,因为您不能在场景中多次使用Node

相关问题