Home > Back-end >  Can't figure out how to setup GridPane at the start of my project without using button layout
Can't figure out how to setup GridPane at the start of my project without using button layout

Time:09-19

I have trouble with adding Pane to GridPane without using a button(by clicking the button in fxml layout). How can I make my app add the squares(white and black table) to the screen from the start instead of waiting for user to click the button I added to layout?

App Class:

public class Application extends javafx.application.Application {

    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("main-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 800, 800);
        stage.setTitle("Chess #1!");
        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch();
    }
}

Here is Controller code:

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class PrimaryController {

    @FXML
    private GridPane gameboard;

    @FXML
    private Button startButton;
    private Pane[][] paneArray;
    private List<Pane> paneList;

    @FXML
    public void start() {
        paneList = new ArrayList<>();
        paneArray = new Pane[8][8];
        for (int i = 0; i < 8; i  ) {
            for (int j = 0; j < 8; j  ) {
                Pane pane = new Pane();
                paneList.add(pane);
                paneArray[i][j] = pane;

                String black = "-fx-background-color: black;",
                        white = "-fx-background-color: white;";

                if (i % 2 == 0) {
                    if (j % 2 == 0) {
                        pane.setStyle(white);
                    } else {
                        pane.setStyle(black);
                    }
                } else {
                    if (j % 2 != 0) {
                        pane.setStyle(white);
                    } else {
                        pane.setStyle(black);
                    }
                }

                gameboard.add(pane, j, i);
            }
        }
    }
}

Here is fxml view:

<VBox alignment="CENTER" spacing="3.0" xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml" fx:controller="com.kuraido.urslanchessengine.PrimaryController">
    <children>
        <GridPane fx:id="gameboard">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints prefHeight="100.0" vgrow="SOMETIMES" />
            </rowConstraints>
            <Button fx:id="startButton" onAction="#start">start</Button>
        </GridPane>
    </children>
</VBox>

CodePudding user response:

From the JavaFX docs (https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#controllers):

when more control over the behavior of the controller and the elements it manages is required, the controller can define an initialize() method, which will be called once on an implementing controller when the contents of its associated document have been completely loaded

What you need to do is to move whole code responsible for generating squares into the overrided initialize() method.

  • Related