2016 年 5 月 27 日更新
Java 8u40 引入了 TextFormatter 类,这是完成此功能的推荐方法(尽管此答案中提供的解决方案仍然有效)。有关详细信息,请参阅Uwe's answer、Hassan's answer 和其他在以下问题中提到 TextFormatter 的答案:
这个问题的另一个答案也有这个解决方案,我没有尝试过,但看起来不错,并且删除了 StackOverflow 版主:
TextField numberField = new TextField();
numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
上面的代码错过了 TextFormatter 的 UnaryOperator 过滤器,您通常也需要该过滤器(否则,该字段将不会显示,将用户输入限制为仅格式化的值,它只允许您通过文本监视未格式化的值格式化程序值属性)。要将解决方案扩展为使用过滤器,可以使用如下代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.stage.Stage;
import javafx.util.converter.NumberStringConverter;
import java.text.ParsePosition;
import java.util.function.UnaryOperator;
public class NumberConverterFieldTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TextField numberField = new TextField();
NumberStringFilteredConverter converter = new NumberStringFilteredConverter();
final TextFormatter<Number> formatter = new TextFormatter<>(
converter,
0,
converter.getFilter()
);
numberField.setTextFormatter(formatter);
formatter.valueProperty().addListener((observable, oldValue, newValue) ->
System.out.println(newValue)
);
stage.setScene(new Scene(numberField));
stage.show();
}
class NumberStringFilteredConverter extends NumberStringConverter {
// Note, if needed you can add in appropriate constructors
// here to set locale, pattern matching or an explicit
// type of NumberFormat.
//
// For more information on format control, see
// the NumberStringConverter constructors
// DecimalFormat class
// NumberFormat static methods for examples.
// This solution can instead extend other NumberStringConverters if needed
// e.g. CurrencyStringConverter or PercentageStringConverter.
public UnaryOperator<TextFormatter.Change> getFilter() {
return change -> {
String newText = change.getControlNewText();
if (newText.isEmpty()) {
return change;
}
ParsePosition parsePosition = new ParsePosition( 0 );
Object object = getNumberFormat().parse( newText, parsePosition );
if ( object == null || parsePosition.getIndex() < newText.length()) {
return null;
} else {
return change;
}
};
}
}
}
运行上述示例时,编辑输入字段并按回车键查看更新的值(更新后的值在更改时输出到System.out)。
有关教程,请参阅:
这与 Urs 引用的解决方案相同,但是我只是将它放在一个完全可执行的程序中以提供上下文中的示例并修改正则表达式(通过在末尾添加 *)以便复制和粘贴工作和它没有 Uluk 提到的问题。该解决方案似乎很简单,可能足以满足大多数目的:
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class NumericTextFieldTest extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
TextField numberField = new TextField() {
@Override public void replaceText(int start, int end, String text) {
if (text.matches("[0-9]*")) {
super.replaceText(start, end, text);
}
}
@Override public void replaceSelection(String text) {
if (text.matches("[0-9]*")) {
super.replaceSelection(text);
}
}
};
stage.setScene(new Scene(numberField));
stage.show();
}
}
替代解决方案
您可能还对我在JavaFX example of binding a slider value to a editable TextField 中的替代解决方案感兴趣。在该解决方案中,我扩展 TextField 以在字段上公开一个 IntegerProperty 以用于简单的绑定目的。替代解决方案类似于原始发布者在他们更新的问题中概述的解决方案(即添加了一个事件过滤器以限制来自关键事件的输入数据),但另外在 TextField 的文本属性上添加了一个 ChangeListener 以确保复制和粘贴的值只有当它们是数字时才被接受。
JavaFX 论坛线程Numeric Textfield in JavaFX 2.0? 中还有一些其他解决方案,其中包括对number fields from FXExperience controls 的引用。