The initial code was not written to account for the panes being added to a parent, and did all calculations based on the Root pane as the parent.
This version determines the absolute offset for the views by traversing up the tree until it finds an AnchorPane. This will obviously fail if you actually add an AnchorPane to the layout (which you shouldn't), but if you absolutely have to, the you will need to amend the code and pass the root pane to the initialize method and test for equivalence in the loop that calculates the absolute ParentX and Y.
Try this one: