Issue
My application needs a text edit area where 'overtype' mode of text entry is desired. That is the sort of behaviour that is triggered in some text entry applications by toggling the "insert" key on the keyboard. In "overtype" mode of text entry, the characters typed by the user replace existing characters if any, rather than pushing them to the left.
How can this be enabled in the JavaFX TextArea?
Solution
Here is an example based on James_D's comment:
public class Main extends Application {
UnaryOperator<TextFormatter.Change> changeFilter = c -> {
if (c.isAdded()) {
int len = c.getText().length();
c.setRange(c.getRangeStart(), Math.min(c.getControlText().length(), c.getRangeStart()+len));
}
return c;
};
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
TextArea textArea = new TextArea("Type over this text.");
textArea.setTextFormatter(new TextFormatter<>(changeFilter));
textArea.setFont(Font.font("Monospaced"));
Scene scene = new Scene(textArea);
stage.setTitle("TypeOver");
stage.setScene(scene);
stage.setMinWidth(400);
stage.show();
}
}
Answered By - swpalmer