-1

I'm working on an application made with JavaFX and CSS. I have reached a point where I have to display a picture inside a scrollPane (which is inside a GridPane). I need to zoom this picture. I succeeded to use the multi-touch features of my trackpad as intended.

But when zooming in, the scrolling bars aren't showing or it stays the way it is at the loading. (The bars are displayed (or not) depending of the size of the picture.)

The same thing happens when zooming out.

Pictures will talk for themselves.

App with a picture loaded. NO zooming. You can notice here, the vertical scroll bar is displayed because the pic is too long.

App with the picture loaded. Zooming in. The scroll bars aren't showing. Except for the vertical one which is the same state.

You can easily see when zooming out, that's the same thing.

So. How can I set the bars to fit the zooming? I want to be able to "move" the picture with my trackpad. (The scrollbars are running with multi-touch gestures and it works.)

Here's a part of my code:

    Tab newTab = new Tab(file.getName());
    newTab.setClosable(true);
    // Create ImageView of Picture
    ImageView imageView = new ImageView();
    // Add ImageView to tab

    scrollPane = new ScrollPane();

    /*
     * ZOOM 
     */

    zoomProperty = new SimpleDoubleProperty(200);

    zoomProperty.addListener(new InvalidationListener() {
        @Override
        public void invalidated(Observable arg0) {
            imageView.setFitWidth(zoomProperty.get() * 4);
            imageView.setFitHeight(zoomProperty.get() * 3);
        }
    });

    scrollPane.setOnZoom(new EventHandler<ZoomEvent>() {
        @Override
        public void handle(ZoomEvent event) {
            imageView.setScaleX(imageView.getScaleX() * event.getZoomFactor());
            imageView.setScaleY(imageView.getScaleY() * event.getZoomFactor());
            event.consume();
        }
    });

    scrollPane.setOnZoomFinished(new EventHandler<ZoomEvent>() {
        @Override
        public void handle(ZoomEvent event) {
            // I want to add something here but don't know what.
            event.consume();
        }
      });

    /*
     * END ZOOM
     */

    imageView.setImage(new Image(file.toURI().toString()));
    imageView.preserveRatioProperty().set(true);
    scrollPane.setContent(imageView);       
    newTab.setContent(scrollPane);
    TabView.addTab(newTab);
}

Thank you very much. Don't hesitate to ask for confirmation or other infos if I forgot some.

kofee
  • 11
  • 1
  • 5

2 Answers2

0

I realized I wasn't using the imageview.setFitWidth() to resize the Imageview depending on the bounds it have a the instant. The scrollpane is adapting itself. It works now, but the zooming is far worst.

kofee
  • 11
  • 1
  • 5
0

Place the ImageView inside a Group and add this group to the ScrollPane.

Thomas
  • 1,622
  • 17
  • 24