how can one detect a finished resizing operation in JavaFX?

XXL picture XXL · Jul 7, 2012 · Viewed 7.6k times · Source

I have a stage, a scene and a WebView node. When I expand the window to a larger size - things get pretty sluggish due to WebView. What I want to do is fill the new space for WebView only when the resizing of the window has been finished (this is me releasing left mouse button on the resizable control/edge of the window). For now I can just set the max. size of this node to what it is by default - this will stop it from expansion. But how can I detect the actual event of a completed resizing operation on the window? With binding, I can verify that the resizing takes place - but it's instantaneous (properties for W & D change immediately w/o releasing LMB), while I only require an action when the LMB has been released. Suggestions?


I tried using an addEventFilter on the stage for Event.ANY, just to see if this event type is recognizable - sadly with no avail.

I've also stumbled upon this unanswered post.

Answer

Uluk Biy picture Uluk Biy · Jul 8, 2012

This is not a direct answer to your question. If I understood the sluggishness correctly you are facing somekind weird flashings and renderings. To reduce the sluggishness the size of the webView can be updated manually and more sparsely:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.PaneBuilder;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class KSO_Demo extends Application {

    @Override
    public void start(Stage primaryStage) {

        final WebView webView = new WebView();
        webView.getEngine().loadContent("<div style='background-color: gray; height: 100%'>Some content</div>");

        final Pane pane = PaneBuilder.create().children(webView).style("-fx-border-color: blue").build();

        final Timeline animation = new Timeline(
                new KeyFrame(Duration.seconds(.5),
                new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent actionEvent) {
                        webView.setPrefSize(pane.getWidth(), pane.getHeight());
                    }
                }));
        animation.setCycleCount(1);

        primaryStage.setScene(new Scene(pane, 300, 250));

        primaryStage.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
                animation.play();
            }
        });

        primaryStage.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
                animation.play();
            }
        });
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The webView is in the Pane which does not layout its children automatically. The webView's size updating is delayed for .5 second ignoring updates in interval.