How can I detect a complete resize operation in JavaFX?

I have stage , scene and WebView node. When I expand the window to a larger size - things get pretty slow due to WebView . What I want to do is fill a new space for WebView only when the window resizing is over (I leave the left mouse button on the resizable control / window edge). For now, I can just set max. the size of this node to what it is by default - this will stop it from expanding. But how can I determine the actual event of a completed window resizing operation? When binding, I can verify that a resizing occurs, but it is instantaneous (the properties for W and D immediately change without releasing the LMB), while I require action only when releasing the LMB . Suggestions?


I tried using addEventFilter in the Event.ANY scene to find out if this type of event was recognized - unfortunately, to no avail.

I also stumbled upon this answer without an answer.

+7
source share
3 answers

This answer only applies if you can use the undecorated Stage for your application.

With an undeclared stage, you can independently handle the scenery and resize operations; allowing you to access the correct hooks to handle the completion of the resize operation.

See the WindowResizeButton class in the source code of the ensemble sample for a demonstration of how to implement a resize marker for an undeclared stage. Modify this class to add a setOnMouseReleased handler and modify the size of the web browser.

I really wonder why this is necessary, although since I didn’t actually have any sluggishness changing the size of the window containing the WebView, it is possible that your application has different uses of the content in the WebView from what I used.

+3
source

This is not a direct answer to your question. If I correctly understood the slowness, you are faced with some strange flashes and visualization. To reduce slowness, the size of the webView can be updated manually and more rarely:

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); } } 

WebView is in Pane , which does not automatically break its children. Web browser size updates are delayed by 0.5 seconds, ignoring updates in the interval.

+2
source

I am making an application where I have to periodically receive satellite images (quite intensively), so I had to find a way to capture only the latest event. I settled on creating a thread to count time for some time before doing an intensive task, and a resize-listener that resets the count. It seems to me more effective for me than planning and completing job deadlines hundreds of times.

Note. I also have some logic for capturing the first window resizing using System.currentTimeMillis ();

 import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; public class ResizeListener implements ChangeListener { long lastdragtime = System.currentTimeMillis(); double xi, yi, dx, dy, wid, hei; GuiModel model; TimerThread timebomb; public ResizeListener(GuiModel model) { this.model = model; timebomb = new TimerThread(350); timebomb.start(); } public void changed(ObservableValue observable, Object oldValue, Object newValue) { if (System.currentTimeMillis() - lastdragtime > 350) { //new drag xi = model.stage.getWidth(); yi = model.stage.getHeight(); model.snapshot = model.canvas.snapshot(null, null); } timebomb.active = true;//start timer timebomb.ms = timebomb.starttime;//reset timer wid = model.stage.getWidth()-72; hei = model.stage.getHeight()-98; dx = model.stage.getWidth() - xi; dy = model.stage.getHeight() - yi; if (dx < 0 && dy < 0) { model.canvas.setWidth(wid); model.canvas.setHeight(hei); model.graphics.drawImage(model.snapshot, -dx/2, -dy/2, wid, hei, 0, 0, wid, hei); } else if (dx < 0 && dy >= 0) { model.canvas.setWidth(wid); model.graphics.drawImage(model.snapshot, -dx/2, 0, wid, hei, 0, 0, wid, hei); } else if (dx >= 0 && dy < 0) { model.canvas.setHeight(hei); model.graphics.drawImage(model.snapshot, 0, -dy/2, wid, hei, 0, 0, wid, hei); } lastdragtime = System.currentTimeMillis(); } private class TimerThread extends Thread { public final int starttime;//multiple of 25 public int ms = 0; public boolean active = false; public TimerThread(int starttime) { this.setDaemon(true); this.starttime = starttime; } public void run() { while (true) { try { Thread.sleep(25); } catch (InterruptedException x) { break; } if (active) { ms -= 25; if (ms <= 0) { active = false; Platform.runLater(() -> { model.canvas.setWidth(wid); model.canvas.setHeight(hei); model.fetchSatelliteImagery(); model.refresh(); }); } } } } }//end TimerThread class }//end listener class 
-one
source

All Articles