I've just started learning JavaFX for building desktop applications and I currently don't have any personal training yet. Right now regarding the question, I'm trying to make a custom layout that depends its size to the overall size of its children. My problem is about getting the size of each child during height and width computation. Here, I'm using the getLayoutBounds()
method to retrieve the children's size, but it always return 0 in my case:
@Override
protected double computePrefWidth(double height) {
for (int i = 0; i < getChildren().size(); i++) {
Node node = getChildren().get(i);
System.out.println(node.getLayoutBounds().getWidth()); // returns 0
}
}
So I guess it is about the layout pass (say, the child is not laid-out yet). But anyways, if that is the case here or not, how can I get the exact size of the node including the insets, padding, etc.? Thanks for all answers.
UPDATE
I end up using the prefWidth()
and prefHeight()
method which is the correct answer. It's my mistake for using the getWidth()
and getHeight()
method as I thought the prefWidth()
method does not include paddings/insets. On the other hand, getWidth()
actually rely on the layout pass, means that, it returns 0 because the nodes (children) are not laid-out yet in the stage, which is what @ᴇʟᴇvᴀтᴇ's answered below is correct.
So for the sake of others whoever come across this, you may want to read this one, and lastly, make sure all CSS stylesheets are properly applied if you are not getting expected result with the pref
, min
, max
methods (see answers by @fabian and @SilasGyeme below).