Search code examples
javafxcursortextarea

JavaFX TextArea setCursor not working


I am having issues getting setCursor() to work properly in TextArea. I am not seeing any other search results at all for issues with this, and might be doing something silly because no one else has addressed this yet. I have tried different options, with no luck. Here are a few attempts below:

Coding this below makes it so only the outer edges are effected by the setCursor.

textArea.setCursor(Cursor.DEFAULT);

In FXML, I get the following if I add it in with Scene Builder.

<TextArea fx:id="textArea" prefHeight="458.0" prefWidth="766.0">
    <font>
        <Font name="System Bold" size="12.0" />
    </font>
    <cursor>
        <Cursor fx:constant="DEFAULT" />
    </cursor>
</TextArea>

It gives me an error, so I add the import...

<?import javafx.scene.Cursor?>

Then it gives me an error, saying "Instances of javafx.scene.Cursor cannot be created by FXML loader." with no hints provided.

I know for ComboBoxes, I have to do the following:

comboBox.getEditor().setCursor(Cursor.DEFAULT);

Is there some way I have to do this for TextArea to work as well?

Thanks!


Solution

  • Your FXML parsed just fine for me, though it didn't have the desired effect. I'm not sure why it gave you errors.

    The reason it doesn't generate the desired cursor is that the Text node is placed as the content of a ScrollPane. The cursor is set by default on that Text node, so it isn't inherited if you set the cursor directly on the TextArea.

    The easiest way to do this is to use an external CSS file:

    .text-area .content {
        -fx-cursor: DEFAULT ;
    }