如何强制插入符号位于后缀之前
在 JavaFx 中,我在 TextField 上应用 TextFormatter,以强制插入符号位于后缀之前(我想创建一个度数字段,因此后缀为 °)。
当我启动应用程序并单击文本字段时,出现以下内容:
我希望插入符永远不会在 ° 之后,如下所示:
这是我的代码: HelloAppplication.java:
package com.example.demo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
HelloController.java:
package com.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
public class HelloController {
@FXML
private TextField input;
@FXML
private void initialize() {
input.setText("°");
input.setTextFormatter(getSuffix("°"));
}
public TextFormatter<String> getSuffix(String suffix) {
return new TextFormatter<>(pChange -> {
int maxPos = pChange.getControlText().length() - suffix.length();
if (pChange.isContentChange()) {
pChange.setRange(Math.min(pChange.getRangeStart(), maxPos), Math.min(pChange.getRangeEnd(), maxPos));
} else {
pChange.setCaretPosition(Math.min(pChange.getCaretPosition(), maxPos));
pChange.setAnchor(Math.min(pChange.getAnchor(), maxPos));
}
return pChange;
});
}
}
hello-view.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/null" fx:controller="com.example.demo.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<children>
<TextField fx:id="input" />
</children>
</VBox>
你有解决方案吗?
In JavaFx, I apply a TextFormatter on a TextField to force the caret to be before a suffix (I want to create a degree field, so the suffix is °).
When I launch my application and click on the textfield, I have the following:
I want the carret to be never after °, like this:
Here is my code:
HelloAppplication.java:
package com.example.demo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
HelloController.java:
package com.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
public class HelloController {
@FXML
private TextField input;
@FXML
private void initialize() {
input.setText("°");
input.setTextFormatter(getSuffix("°"));
}
public TextFormatter<String> getSuffix(String suffix) {
return new TextFormatter<>(pChange -> {
int maxPos = pChange.getControlText().length() - suffix.length();
if (pChange.isContentChange()) {
pChange.setRange(Math.min(pChange.getRangeStart(), maxPos), Math.min(pChange.getRangeEnd(), maxPos));
} else {
pChange.setCaretPosition(Math.min(pChange.getCaretPosition(), maxPos));
pChange.setAnchor(Math.min(pChange.getAnchor(), maxPos));
}
return pChange;
});
}
}
hello-view.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/null" fx:controller="com.example.demo.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<children>
<TextField fx:id="input" />
</children>
</VBox>
Have you got a solution ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论