I want to change the TextArea cursor, but nothing happens when I use this:
numTextArea.setCursor(Cursor.DISAPPEAR);
The default skin of TextArea
includes a "content area", which is documented as a Region
. It's this content area that has its cursor set to Cursor.TEXT
(by the default user agent stylesheet). And since the mouse is hovering over the content area, not the text area directly, you see the cursor of the content area instead of the text area.
The likely easiest way to change the cursor is to use CSS.
.text-area .content {
-fx-cursor: disappear;
}
Then add the stylesheet to the scene (or the text area, or an ancestor layout). For example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class Main extends Application {
private static final String CSS = """
.text-area .content {
-fx-cursor: hand;
}
""".stripIndent();
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(new TextArea(), 500, 300);
scene.getStylesheets().add("data:text/css," + CSS);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Note: The above adds the stylehseet to the scene via a data URL. This requires JavaFX 17+.
The example uses Cursor.HAND
because Cursor.DISAPPEAR
just gave me the default cursor on Windows 10 with Java/JavaFX 20.0.1. Not sure if that's a bug.
Note you can use null
or inherit
for -fx-cursor
in the CSS, and then set the text area's cursor in code (like you're currently trying to do).