I have some sort of visualization having elements that have ports that can be connected with each other. The ports of an element are children of the javafx pane that represents an element. I was now wondering what could be the best way to bind start and endpoint of connecting lines to the locations of the ports because the ports may be located quite deep in a hierarchy of elements. Thats why I was thinking that I may need the absolute positions of these ports, but did not find an easy way to get them. Right now, the lines are directly under the scene root btw.
1 Answers
Its hard to say without seeing structure of your code. But I had similar problem while working on uml modeling tool written in javafx so my solution might help you.
So in your case I would do something like this:
Implement
Port
in such way that they haveDoubleProperty
for their X and Y location, now depending on how it should look implementation might be different, in my case I usedCircle
and itscenterXProperty
andcenterYProperty
, another option if using some rectangular shape is to create owncenterXProperty
andcenterYProperty
, they can be really easy to calculate by binding tolayoutX
+ (width
/ 2), same for y just using height. It should look something like this:DoubleProperty centerXProperty = new SimpleDoubleProperty(0);
centerXProperty.bind(n.layoutXProperty().add(n.widthProperty().divide(2)));
Where
n
is some Node on scenegraph, you might useprefWidthProperty
as well in case you are using custom width/heightYour pane should have list of all ports, now depending on how you want to make this
Port
s it might differ but in my project I would createPort
on intersection ofLine
that user draws andPane
that it is connected to, you could also just put predefinedPorts
on everyPane
and only make connection ifLine
end point is insidePort
radius.Line already has
endXProperty
andendYProperty
so you can simply bind them to location properties of yourPort
, this way you can easy allow moving them together.
If you need code example then you need to provide what you got so far so I can help you with that.
EDIT
Regarding parent-child relation, our solution for root of diagram was to extend anchor pane as he gives most control to its children, every node on this anchor pane had to be UmlElement
, which further down spreads to UmlNode
and UmlEdge
, then UmlNode
has sub-types ParentUmlNode
and ChildUmlNode
, Child
can be put inside Parent
and their position is bound to Parent
position, it doesn't care about outside, because of that once Parent
moves his children follow him respectively.
IMHO this is much better than to handle deep child position based on root of your view, as there might be some problems keeping everything sync especially if you add resizing, multi-select and similar more complex features.

- 2,661
- 4
- 22
- 31
-
So you would say that I should not try to control layouting via the parent/child relation for my situation? Because if port is a child of element, i dont need to bind or add the layoutXProperty because the bindings would be already relative to the parent. But then I have no absolute values and my current problem occures. I did the same "workaround" as you with not having the parent/child relationship and binding on the e.g. layoutXProperty to have absolute positioning, but I thought this may be bad style. – Magnus Lutz Jun 04 '18 at 16:20
-
Not really sure what you mean under parent/child relation, can you elaborate on that? – FilipRistic Jun 04 '18 at 16:42
-
If you have a Pane and add some child node to it via getChildren().add(..) the layoutXProperty of this child will be relative to the parent pane. In that case, getting the absolute position of such a child within a scene or relative to some ancestor proves to be much more difficult or at least I did not see a function like xBindingRelativeTo(Node). The solutions I found were similar to https://stackoverflow.com/a/31150013/4285113 but I think this approach does not get along well with property binding of x/y properties – Magnus Lutz Jun 05 '18 at 09:23