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).
The default skin of
TextAreaincludes a "content area", which is documented as aRegion. It's this content area that has its cursor set toCursor.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.
Then add the stylesheet to the scene (or the text area, or an ancestor layout). For example:
Note: The above adds the stylehseet to the scene via a data URL. This requires JavaFX 17+.
The example uses
Cursor.HANDbecauseCursor.DISAPPEARjust 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
nullorinheritfor-fx-cursorin the CSS, and then set the text area's cursor in code (like you're currently trying to do).