Issue
My task is creating the resizable graph in full screen mode. Graph must be resizable when user change the window of program that is fullscreen by default (the components and lines of graph change their sizes). I realize the graph with AnchorPane
: the elements (that are GridPane
s) stay in coordinates that are defined. Then I make the lines with the help of method getBoundsInParent()
. Here is the scema of graph:
All is good but the problem is that I can t resize my graph. All components are stayed with their sizes; variables prefSize
, minSize
, maxSize
don't do the resizing. I try using the params AnchorPane.setTopAnchor
etc., but they don t resize, only move the GridPane
component.
Also I try to use GridPane
as the layout instead the AnchorPane
. But my lines that are binded with the methods component.getBoundsInParent()
fly away in random positions (I understand that the getBoundsInParent()
method returns other coordinates that with GridPane
).
My project is located at work computers without Internet & I can t show it. I think the way of binding the lines between graphs is useful to show in code block because it is the cause of moving out the lines when the components are in GridPane layout:
line.startXProperty().bind(source.layoutXProperty().add(source.getBoundsInParent().getWidth() / 2.0));
line.startYProperty().bind(source.layoutYProperty().add(source.getBoundsInParent().getHeight() / 2.0));
line.endXProperty().bind(target.layoutXProperty().add(target.getBoundsInParent().getWidth() / 2.0));
line.endYProperty().bind(target.layoutYProperty().add(target.getBoundsInParent().getHeight() / 2.0));
What is the way to resize the graph with elements that I create and lines that are connected with this elements. May be it s the properties of AnchorPane
or GridPane
? Or some binding of start & endpoints of lines?
Solution
Bindings need to be based on properties or other ObservableValue implementations, so changes to their values are properly tracked. Direct method calls like source.getBoundsInParent().getWidth() / 2.0
are only evaluated once, the moment that code creates the binding, so changes to the width are never seen.
line.startXProperty().bind(
source.layoutXProperty().add(
source.widthProperty().divide(2)));
line.startYProperty().bind(
source.layoutYProperty().add(
source.heightProperty().divide(2)));
If source
and target
are not Regions and thus don’t have a width
property, you can use Bindings to create a dynamic binding of their bounds:
DoubleBinding width = Bindings.createDoubleBinding(
() -> source.getLayoutBounds().getWidth(),
source.layoutBoundsProperty());
line.startXProperty().bind(
source.layoutXProperty().add(
Bindings.createDoubleBinding(width.divide(2)));
DoubleBinding height = Bindings.createDoubleBinding(
() -> source.getLayoutBounds().getHeight(),
source.layoutBoundsProperty());
line.startYProperty().bind(
source.layoutYProperty().add(
Bindings.createDoubleBinding(height.divide(2)));
Answered By - VGR