简体   繁体   中英

JavaFX - Override WebView's Cursor Change

In the process of creating my own web browser application in Java using the JavaFX library I found that WebView tends to override cursor changes made by other classes. Since I am using an UNDECORATED window type, my plan is to create a custom resizing class to mimic window resizing functionality; this involves changing the cursor to the "RESIZE" type. This does work, however since the WebView element has no border and does not plan to; it overrides the cursor change. I was unable to find an event for WebView's cursor update.

In short: How can I forcefully change the cursor (overriding WebView).

Note: I have attempted to change the cursor back after WebView modifies it (I found no way to consume the change) but this resulted in the following error:

java.lang.StackOverflowError
at javafx.scene.Node$MiscProperties$6.invalidated(Node.java:6459)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
at javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:82)
at javafx.scene.Node.setCursor(Node.java:1160)
at to.us.thedjcreeper.thelightweb.nodes.WebTab.lambda$new$1(WebTab.java:28)
at to.us.thedjcreeper.thelightweb.nodes.WebTab$$Lambda$98/1896727623.changed(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
at javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:82)
at javafx.scene.Node.setCursor(Node.java:1160)

Code:

webView.cursorProperty().addListener((observable, oldValue, newValue) -> {
    if (ResizeHelper.resizing && oldValue != newValue) webView.setCursor(oldValue);
});

You can add a border by placing the WebView in a StackPane, where you can set its margin. To accomplish the cursor change, add another pane underneath which can provide the cursor and can field mouse drag events:

double resizeBorderThickness = 6;
double cornerSize = 24;

Region n = new Region();
n.setCursor(Cursor.N_RESIZE);
n.setMinHeight(resizeBorderThickness);
Region s = new Region();
s.setCursor(Cursor.S_RESIZE);
s.setMinHeight(resizeBorderThickness);
Region e = new Region();
e.setCursor(Cursor.E_RESIZE);
e.setMinWidth(resizeBorderThickness);
Region w = new Region();
w.setCursor(Cursor.W_RESIZE);
w.setMinWidth(resizeBorderThickness);

Region nw = new Region();
nw.setCursor(Cursor.NW_RESIZE);
nw.setMinSize(cornerSize, cornerSize);
Region ne = new Region();
ne.setCursor(Cursor.NE_RESIZE);
ne.setMinSize(cornerSize, cornerSize);
Region sw = new Region();
sw.setCursor(Cursor.SW_RESIZE);
sw.setMinSize(cornerSize, cornerSize);
Region se = new Region();
se.setCursor(Cursor.SE_RESIZE);
se.setMinSize(cornerSize, cornerSize);

GridPane resizePane = new GridPane();
resizePane.addRow(0, nw, n, ne);
resizePane.addRow(2, sw, s, se);
resizePane.add(w, 0, 1);
resizePane.add(e, 2, 1);

GridPane.setHgrow(n, Priority.ALWAYS);
GridPane.setHgrow(s, Priority.ALWAYS);
GridPane.setVgrow(w, Priority.ALWAYS);
GridPane.setVgrow(e, Priority.ALWAYS);

StackPane stackPane = new StackPane(resizePane, webView);
StackPane.setMargin(webView, new Insets(resizeBorderThickness));

Scene scene = new Scene(stackPane);

This is built into WebVew:

webView.getEngine().inheritCursor(false);

This will disallow the webView from changing the cursor and allow you to modify the cursor. However, I would suggest that you only set this to false when you need to modify the cursor type.

Documentation: https://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/InheritCursor.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM