0
votes

I'm new in JavaFx, i have two gridPane created in FXMl, i want to do the same thing but in Javafx.

I want to create class that extends GridPane, so when i call my class i will have the same result that i have in fxml PS:if you are using sceneBuiler, check Grid Lines visible so you can see all GridPane thank you

  <GridPane layoutX="138.0" layoutY="72.0">
    <children>
    <Label text="01" GridPane.columnIndex="1" GridPane.rowIndex="0" />

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

    <Label text="01" GridPane.columnIndex="3" GridPane.rowIndex="0" />

    <Label text="01" GridPane.columnIndex="4" GridPane.rowIndex="0" />

    <Label text="01" GridPane.columnIndex="0" GridPane.rowIndex="0" />

  </children>

  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

  </columnConstraints>

  <rowConstraints>

    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

  </rowConstraints>

</GridPane>

<GridPane layoutX="38.0" layoutY="102.0">

  <children>

    <Label prefWidth="74.0" text="Label" GridPane.columnIndex="0" GridPane.rowIndex="0" />

  </children>

  <columnConstraints>

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" />

  </columnConstraints>

  <rowConstraints>

    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

  </rowConstraints>

</GridPane>
1
Why you don't load your fxml to display your gridpane ?? - agonist_
I need to do it in Javafx - firar binazer

1 Answers

0
votes

Here is a routine I used to create a GridPane x by y and add it in a parent Pane called grille (This pane is created by FXML)

public GridPane DessineGrille(int x, int y) {
    ((Pane) Main.root.lookup("#grille")).getChildren().clear();
    GridPane gp = new GridPane();
    x += 2;
    ColumnConstraints column = new ColumnConstraints();
    column.setPercentWidth((1.0f/((float) x))*100.0f);
    for (int i = 1; i <= x; i++) {
        gp.getColumnConstraints().add(column);
    }
    y += 2;
    RowConstraints line = new RowConstraints();
    line.setPercentHeight((1.0f/((float) y))*100.0f);
    for (int i = 1; i <= y; i++) {
        gp.getRowConstraints().add(line);
    }
    gp.setGridLinesVisible(true);
    gp.setHgap(10);
    gp.setVgap(10);
    Light.Distant light = new Light.Distant();
    light.setAzimuth(-135.0);

    Lighting lighting = new Lighting();
    lighting.setLight(light);
    lighting.setSurfaceScale(5.0);
    gp.setEffect(lighting);
    gp.setMinSize(((Pane) Main.root.lookup("#grille")).getWidth(), ((Pane) Main.root.lookup("#grille")).getHeight());
    ((Pane) Main.root.lookup("#grille")).getChildren().add(gp);
    return gp;

}

To help you, in french 'dessine' means draw and 'grille' menas grid (I don't want to introduce error just by translating).

Does it help?