Johannes Mols Johannes Mols - 1 month ago 15
Java Question

JavaFX: Setting a border within a rectangle to keep the width and height

So, I have a grid of rectangles in my scene and I want to give it a border to visually seperate it to the others. I am currently using

grid.setGridLinesVisible(true);
and that works fine, but I am sure this isn't supposed to be used for that.

I tried setting a border with
nodes[j][i].setStroke(Color.BLACK);
and this works too, but now my whole grid is getting a lot bigger because it's drawing the border on the outside of the rectangle and therefore resizing it.

Is there a way to draw a border/stroke inside of a rectangle to keep the transformation?
I already looked up the documentation but there isn't such a function.

Thanks for the help!

Answer

You can use setStrokeType for your rectangles to draw the border inside using StrokeType.Inside:

nodes[j][i].setStrokeType(StrokeType.Inside);