JavaFx TextArea 中的改写模式:如何启用
Overtype mode in JavaFx TextArea: How to enable
我的应用程序需要一个文本编辑区域,其中需要 'overtype' 文本输入模式。这是在某些文本输入应用程序中通过切换键盘上的“插入”键触发的行为。在文本输入的“改写”模式下,用户键入的字符会替换现有字符(如果有),而不是将它们推到左侧。
如何在 JavaFX TextArea 中启用此功能?
这是一个基于 James_D 的评论的示例:
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();
}
}
我的应用程序需要一个文本编辑区域,其中需要 'overtype' 文本输入模式。这是在某些文本输入应用程序中通过切换键盘上的“插入”键触发的行为。在文本输入的“改写”模式下,用户键入的字符会替换现有字符(如果有),而不是将它们推到左侧。
如何在 JavaFX TextArea 中启用此功能?
这是一个基于 James_D 的评论的示例:
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();
}
}