I want to implement a JavaFX layout component. The final goal is for that component to allow a sandwich-like stacking of several components, then to clip this stack and place the resulting clipped node into standard layout components.
When I implemented the component all went good, with one exception: I am not able to properly return the component's layoutBounds. I invested a lot of time googling and researching but did not find (Google) nor understand (in the JavaFX source code) how to report a selfmade component's layout bounds.
Some details: Node.getLayoutBounds() is final, so cannot be overridden. The layoutBoundsProperty is read-only and cannot be modified. In the JavaFx Node implementation are many helper operations and seemingly helpful comments, but all refer to package-private functionality, not accessible from the outside.
So the question is: How to implement a component so that it can compute its own layout bounds and report it in calls like getLayoutBounds() to its context?
Below is executable source code. Not sure if it helps a lot, but it asks the right questions at the right places.
package stackoverflow.demo;
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class LayoutBoundsTest extends Application {
public static void main(String[] args) {
Application.launch(args);
}
/**
* How to implement this component so that it can return
* its own computed layout bounds?
*/
public static class CenterPane extends StackPane
{
public CenterPane()
{
}
@Override
public boolean isResizable()
{
return false;
}
protected void addLayoutComponent( Node node )
{
Bounds bounds = node.getLayoutBounds();
// How to set 'bounds' on this CenterPane so that
// they are reported via CenterPane.getLayoutBounds()?
getChildren().add( node );
}
}
@Override
public void start(Stage stage) {
CenterPane centerPane = new CenterPane();
centerPane.addLayoutComponent( new Circle( 30, Color.YELLOW ) );
Node circle = new Circle( 30, Color.GREEN );
VBox root = new VBox();
root.getChildren().addAll(centerPane, circle);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Testing LayoutBounds");
stage.show();
// Both calls below should report the same layout bounds.
System.out.println("b0=" + centerPane.getLayoutBounds());
System.out.println("b2=" + circle.getLayoutBounds());
}
}