In this post, I present the steps that allow you to hide or change the mouse cursor shape in the entire JavaFX application. You will find solutions for the problems that I have encountered so far by implementing the requirement. In particular, you will learn how you can detect and porform the change of the cursor in the WebView control.
- Changing the mouse cursor in any JavaFX control
- Changing the mouse cursor in the WebView control
Changing the mouse cursor in any JavaFX control
Let’s consider the HelloWorld JavaFX application shown below. It consists of a class called Main, which is also a acontroller for sample.fxml form. In the class you can find handles for Pane and WebView controls, as well as the initialize method, which is executed as soon as the form is ready to initialize.
package sample; import javafx.application.Application; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Cursor; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @FXML private Pane myPane; @FXML private WebView myWebView; @FXML private void initialize() { } @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Hello World"); Scene scene = new Scene(root, 300, 275); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.Pane?> <?import javafx.scene.web.WebView?> <HBox fx:controller="sample.Main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1"> <children> <Pane prefHeight="200.0" prefWidth="200.0" fx:id="myPane"/> <WebView prefHeight="200.0" prefWidth="200.0" fx:id="myWebView" /> </children> </HBox>
package sample; import javafx.application.Application; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Cursor; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @FXML private Pane myPane; @FXML private WebView myWebView; @FXML private void initialize() { } @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Hello World"); Scene scene = new Scene(root, 300, 275); scene.setCursor(Cursor.NONE); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
package sample; import javafx.application.Application; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Cursor; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @FXML private Pane myPane; @FXML private WebView myWebView; @FXML private void initialize() { myPane.setCursor(Cursor.OPEN_HAND); } @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Hello World"); Scene scene = new Scene(root, 300, 275); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Changing the mouse cursor in the WebView control
The WebView component plays the role of a web browser in JavaFX. Therefore, its main task is to display HTML pages. In the case of the WebView control, the problem of hiding or changing the cursor becomes a bit more complicated, because the mouse cursor changes quite often and additionally calling the setCursor method on the WebView component does not seem to be effective.
However, this is not entirely true. In fact, by calling the myWebView.setCursor(Cursor.NONE) method, we hide the cursor, but the movement of the mouse causes that it adapts to the content of the page being viewed, changing to the appropriate one (eg pointing to the link).
package sample; import javafx.application.Application; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Cursor; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @FXML private Pane myPane; @FXML private WebView myWebView; @FXML private void initialize() { myPane.setCursor(Cursor.CROSSHAIR); myWebView.setCursor(Cursor.MOVE); myWebView.cursorProperty().addListener((observable, oldValue, newValue) -> { myWebView.setCursor(Cursor.MOVE); }); } @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Hello World"); Scene scene = new Scene(root, 300, 275); scene.setCursor(Cursor.OPEN_HAND); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }