Skin layoutChildren: when to get bounds of child nodes?

Martin Sladecek martin.sladecek at oracle.com
Wed Jul 30 15:39:33 UTC 2014


On 07/30/2014 05:12 PM, Werner Lehmann wrote:
>
> layoutChildren() {
>   ...
>   bip = child1.getBoundsInParent()
>   if (bip.getMinX() == 0 && bip.getWidth() == 0) {
>     vbox.layout();
>     bip = child1.getBoundsInParent();
>   }
>   ...
> }
I assume you don't change "child1" Nodes, so it should work.

> It sure does. I have created pane-based controls with manual layouting 
> before but it is some work to get it right, especially with the 
> computeMinPrefMaxWidthHeight methods (sometimes +baseline), snapsize 
> etc pp. I figured it is not worth the hassle here if a vbox and hbox 
> do 95% of what I need :)  A cleaner approach would be to make child2 
> unmanaged as you suggest but then I need to reserve vertical space in 
> its vbox container, currently done with css padding on the region 
> itself. For the time being I have to stick with this and move on.

Yeah, one problem with layouts is when you want to do some layout that 
is almost the same as some predefined layout, but you need to tweak it 
somehow. Maybe in the future we could add some abstraction and extract 
some (parts of) the algorithms we use in our layout containers so they 
can be reused in custom layouts.
If you reuse some predefined layout, you'll probably end up with some 
hacky code since you'll usually mess up with the inputs for the layout. 
The rule of thumb is to use some more complex (GridPane) or free form 
(AnchorPane) layout instead of trying to slightly tweak some simple layout.
In your case, wouldn't GridPane do the job? Basically, you have a row of 
text nodes and you need a second row where a single rectangle is in the 
same column as currently selected (?) text node. The size of the 
rectangle can be set to fillWidth the column, so GridPane will do all 
the work for you.

-Martin


More information about the openjfx-dev mailing list