0
package com.gui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import org.w3c.dom.Text;

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

public class LayoutController {
    @FXML
    Pane boardPane;

    @FXML
    Button btnPlayAgain;

    @FXML
    TextField message;

    public void btnPlayAgain(ActionEvent actionEvent) {
        message.setText("Testing...");
    }

    @FXML
    public void initialize() {
        message.setText("Working");
        int w = (int) boardPane.getWidth();
        int h = (int) boardPane.getHeight();
        int grid = 12;
        int scaleW = w / grid;
        int scaleH = h / grid + 5;

        for (int i = 0; i < w; i += scaleW) {
            for (int j = 0; j < h; j += scaleH) {
                Rectangle rectangle = new Rectangle(i, j, scaleW, scaleH);
                rectangle.setFill(Color.LIGHTYELLOW);
                rectangle.setOpacity(2);
                rectangle.setStroke(Color.BLACK);
                boardPane.getChildren().add(rectangle);
            }
        }
    }
}

My FXML :

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane minHeight="300" minWidth="300" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gui.LayoutController">
    <Pane prefHeight="50.0" prefWidth="600.0">
        <TextField layoutX="100.0" layoutY="14.0" />
        <TextField layoutX="375.0" layoutY="14.0" />
        <Label layoutX="285.0" layoutY="15.0" text="Player 2" textFill="#000dff">
            <font>
                <Font name="System Bold" size="17.0" />
            </font>
        </Label>

        <Label alignment="TOP_LEFT" contentDisplay="RIGHT" layoutX="10.0" layoutY="15.0" text="Player 1" textFill="#ee0303">
            <font>
                <Font name="System Bold" size="17.0" />
            </font>
        </Label>
    </Pane>

    <Pane fx:id="boardPane" layoutY="50.0" minHeight="300.0" minWidth="600.0" prefHeight="300.0" prefWidth="600.0" />

    <Pane fx:id="paneBottom" layoutY="354.0" prefHeight="42.0" prefWidth="600.0">
        <Button fx:id="btnPlayAgain" layoutX="5.0" layoutY="10.0" mnemonicParsing="false" onAction="#btnPlayAgain" text="Play Again" />
        <TextField fx:id="message" layoutX="100.0" layoutY="10.0" prefWidth="450.0" editable="false"/>
    </Pane>
</AnchorPane>

It should show something like that : Image 1

But it is showing something like that : Image 2

Please help anybody can help... :)

I want show the gird everytime i run code but nothing is showing in the pane layout I have tried different time but nothing seems to work your insight to my problem will be very helpful...

0009laH
  • 1,960
  • 13
  • 27
  • 2
    the size of nodes is 0 until they are shown .. – kleopatra Jul 16 '21 at 08:41
  • .. and initialize is called before, so w and h are 0 (and consequently all sizes/location calculated from them as well) – kleopatra Jul 16 '21 at 08:49
  • is there any way to do what i am trying to do ? – Aniket Verma Jul 16 '21 at 09:16
  • 1
    wait until the pane is added to the stage (f.i. in a listener to its scene property) or hard-code the rectangle sizes, the layout will follow - provided you _do not_ hard-code sizing hints in the fxml :) – kleopatra Jul 16 '21 at 09:21
  • just tested: the sceneProperty is still too early, listen to layoutBoundsProperty instead .. – kleopatra Jul 16 '21 at 09:36
  • 2
    Depending on what you're actually trying to do, it may be better to use a layout pane (e.g. a `GridPane`) and add `Pane`s to it instead of `Rectangle`s. Or use a custom subclass of `Pane` for your `boardPane` and override `layoutChildren()`. – James_D Jul 16 '21 at 11:47
  • Something that is probabaly not recommend but doable. You could hard code your height and width. Change `int w = (int) boardPane.getWidth(); int h = (int) boardPane.getHeight();` to `int w = 600; int h = 300;` – SedJ601 Jul 16 '21 at 15:43

0 Answers0