I'm trying to resize the caret in a TextField without modifying the font size, but I can not do it.
Does anyone know how to do that?
Thanks!
I'm trying to resize the caret in a TextField without modifying the font size, but I can not do it.
Does anyone know how to do that?
Thanks!
I have no idea why you would want to do this. And this probably won't give you exactly what you want, but it does change the size of the caret. Note that the big caret will still be clipped to be within the bounds of the containing TextField.
The small line between the m and the a is the small caret. Image transfer into stackoverflow reduced clarity so it is hard to see:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class BiglyCaret extends Application {
@Override
public void start(Stage stage) throws Exception {
TextField bigCaretTextField = new TextField("Big Caret");
bigCaretTextField.setSkin(
new TextFieldCaretControlSkin(
bigCaretTextField,
2
)
);
TextField smallCaretTextField = new TextField("Small Caret");
smallCaretTextField.setSkin(
new TextFieldCaretControlSkin(
smallCaretTextField,
0.5
)
);
TextField normalTextField = new TextField("Standard Caret");
VBox layout = new VBox(
10,
bigCaretTextField,
smallCaretTextField,
normalTextField
);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
TextFieldCaretControlSkin.java
Note, this uses a private API class in Java 8, so it won't work for Java 9. In Java 9, the TextFieldSkin will become public API (move to a javafx
package instead of a com.sun
package), so it will probably work in Java 9 if you change the package.
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.control.TextField;
public class TextFieldCaretControlSkin extends TextFieldSkin {
public TextFieldCaretControlSkin(TextField textField, double scale) {
super(textField);
setScale(scale);
}
private void setScale(double scale) {
caretPath.setScaleX(scale);
caretPath.setScaleY(scale);
}
}
This answer was based on this answer: