javafxlayoutgridpanehbox

HBox doesn't fill parent GridPane when rotated by 90 degrees


I have created a gridpane as seen in the image and to this gridpane I add multiple HBoxes for the top row I have to rotate the HBox but when I do this the HBox doesn't fill the gridpane anymore for some reason.

I've tried manually changing the height of the HBox but this also changes the width of the cell of the gridpane in which the HBox is located.

I wondered if anyone knew how to make the rotated HBox still fill its parent.

The orange tiles are the HBoxes in a program meant to emulate Monopoly.

monopoly

So here I will be defining my own HBox class by the name of StreetTile which is an orange tile in the image:

public class Tile extends HBox {

    public Tile(int orientation){
        //orientation = 0,1,2 of 3
        setAlignment(Pos.CENTER_RIGHT);
       
        setRotate(90*orientation);
    }
}

public class RectangleTile extends Tile{
    public RectangleTile(int orientation) {
        super(orientation);
        setMaxHeight(66);
        setPrefHeight(66);
        setFillHeight(true);

        setMaxWidth(128);
        setPrefWidth(128);

        setStyle("-fx-background-color: ORANGE");
    }
}

public class StreetTile extends RectangleTile{
    public StreetTile(String Name, Color color, int orientation) {
        super(orientation);
        Label label = new Label(Name);
        label.setAlignment(Pos.CENTER);


        getChildren().add(label);
        getChildren().add(new Rectangle(22,66, color));
        //The rectangle represents the monopoly street color
    }
}

And then I just add it to my gridpane which only has its alignment set to center.


Solution

  • You want to use the transformed bounds of the node for layout calculation. To do that, wrap the node in a Group:

    node.setRotate(90);
    Group group = new Group(node);
    

    The layout bounds of the group now match the bounds of the rotated node.

    From the Group documentation (emphasis mine):

    Any transform, effect, or state applied to a Group will be applied to all children of that group. Such transforms and effects will NOT be included in this Group's layout bounds, however if transforms and effects are set directly on children of this Group, those will be included in this Group's layout bounds.