I'm looking to customize the caret color for all JavaFX text inputs (such as TextField, TextArea, ComboBox:editable, DatePicker, etc.). I came across a solution on Stackoverflow: How to change the caret color in JavaFX 2.0?
There's also an example on GitHub. However, the first one changes both text and caret colors, which is not ideal. The second method involves extending the TextFieldSkin class, but how can it be used in CSS?
I would appreciate any assistance with this issue.
UPDATE 1:
I discovered the CSS style property for JavaFX controls: -fx-skin
.
This theoretically allows for setting a custom skin class (-fx-skin: "package.MySkin";
), but the skin class doesn't seem to be applied. Here's what the class looks like:
package gui;
…
public class MyTextFieldSkin extends TextFieldSkin
{
public MyTextFieldSkin(TextField tf) {
super(tf);
System.out.println("MyTextFieldSkin constructor called!");
ReadOnlyObjectWrapper<Color> color = new ReadOnlyObjectWrapper<>(Color.green);
caretPath.strokeProperty().bind(color);
caretPath.setStrokeWidth(1.5);
}
}
It is referenced in CSS as follows:
.text-field {
-fx-skin: "gui.MyTextFieldSkin";
}
What am I missing here? I checked AquaFX's source code, and they seem to be using a similar approach!