0
votes

I need a scaled ImageView with a Canvas overlay, stretched over the parent, with a constant margin on the right hand side. This suggests an AnchorPane with the ImageView and Canvas as children, each with a rightAnchor inset representing the margin.

I need to put the AnchorPane in a ScrollPane. The ScrollPane must, when resized, resize both the ImageView and the Canvas overlay within the AnchorPane, while retaining the margin.

I have tried adding the ImageView and Canvas to the AnchorPane, and adding the AnchorPane to the ScrollPane, with ScrollPane.setFitToWidth(true). When I increase the size of the ScrollPane, all works well - both the ImageView and Canvas expand to fill the ScrollPane, retaining the margin on the right hand side.

However, when I reduce the size of the ScrollPane, the size of the AnchorPane is not reducing, but instead the viewport over the AnchorPane is reducing, with increasing scrollbars.

NB, an ImageView inserted directly into the ScrollPane does work - it rescales both on grow and shrink.

It looks as though ScrollPane.setFitToWidth behaviour differs depending on the container inside the ScrollPane.

Is this correct, and can anyone suggest a solution, please?

Robert

2

2 Answers

0
votes

Try adding a listener to one of the ScrollPane's Bounds properties and reduce the AnchorPane's size from there when appropriate.

0
votes

Solved using Jurgen's answer.

I overrode AnchorPane.computeMinHeight, computePrefHeight and computeMaxHeight to return imageView.minHeight, prefHeight and maxHeight. This ties the anchor pane's height to the imageView's height - which the imageView calculates from its own width.

Then I added a listener to the scrollPane.widthProperty which calls the AnchorPane.setPrefWidth, setMinWidth and setMaxWidth

In this way, the anchor pane is correctly signalling its preferred, minimum and maximum width to the enclosing scrollPane.