我需要在我的用户界面中插入一个数字字段。因此,我需要检查文本字段上的关键事件,以检查输入字符是否为数字。我通过扩展TextField创建了一个类。如果TextField类中有一个处理keyEvents的方法,我可以简单地重写该方法以适用于数字字段。有任何想法吗?
谢谢
我需要在我的用户界面中插入一个数字字段。因此,我需要检查文本字段上的关键事件,以检查输入字符是否为数字。我通过扩展TextField创建了一个类。如果TextField类中有一个处理keyEvents的方法,我可以简单地重写该方法以适用于数字字段。有任何想法吗?
谢谢
2016年5月27日更新
Java 8u40引入了TextFormatter类,这是推荐的实现此功能的方法(尽管本答案提供的解决方案仍然有效)。有关更多信息,请参见Uwe's answer、Hassan's answer以及其他提到以下问题的答案中使用TextFormatter:
还有另一个答案中提供的解决方案,我没有尝试过,但看起来很好,被StackOverflow的一位管理员删除了:
TextField numberField = new TextField();
numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
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
)。*
),以便于复制和粘贴,并且不会出现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,以便进行简单的绑定。备用解决方案类似于原始帖子中更新的问题概述(即添加事件过滤器以限制来自键事件的输入数据),但另外还添加了一个ChangeListener在TextField的文本属性上,以确保只有数字才能被接受复制和粘贴的值。
在JavaFX论坛线程Numeric Textfield in JavaFX 2.0?中还有其他一些解决此问题的解决方案,其中包括对number fields from FXExperience controls的引用。
在FXExperience上有一个提示,与此类似的问题都可以解决。
简单说,您需要扩展TextField
并覆盖replaceText()
和replaceSelection()
方法,过滤掉所有不是数字的输入。
一旦实现,这两种方法应遵循以下模板:
if (!newText.matches("[0-9]")) {
super.call(allParams)
}
TextField
无法避免粘贴限制的值到其中,正如教程中所讨论的那样。 - Uluk Biypublic class NumFieldFX extends TextField {
public NumFieldFX() {
this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
public void handle( KeyEvent t ) {
char ar[] = t.getCharacter().toCharArray();
char ch = ar[t.getCharacter().toCharArray().length - 1];
if (!(ch >= '0' && ch <= '9')) {
System.out.println("The char you entered is not a number");
t.consume();
}
}
});
}
}
Arrays.asList(txtLongitude, txtLatitude, txtAltitude, txtSpeed, txtOrientation).forEach(textField ->
textField.textProperty().addListener((observable, oldValue, newValue) ->
textField.setText(newValue.matches("^[0-9]*\\.?[0-9]*$") ? newValue : oldValue)
));
这是我编写的 CustomText 字段。它可以处理仅数字输入和最大尺寸。这是一个自定义控件,可用于 FXML,并且属性可以在 FXMl 中进行设置。
package fxml;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.IntegerPropertyBase;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.TextField;
public class CustomTextField extends TextField {
/**
* numericOnly property if set, will allow accept only numeric input.
*/
private BooleanProperty numericOnly = new SimpleBooleanProperty(this,
"numericOnly", false);
public final boolean isNumericOnly() {
return numericOnly.getValue();
}
public final void setNumericOnly(boolean value) {
numericOnly.setValue(value);
}
public final BooleanProperty numericOnlyProperty() {
return numericOnly;
}
/**
* maxSize property , determines the maximum size of the text that can be
* input.
*/
public IntegerProperty maxSize = new IntegerPropertyBase(1000) {
@Override
public String getName() {
return "maxSize";
}
@Override
public Object getBean() {
return CustomTextField.this;
}
};
public final IntegerProperty maxSizeProperty() {
return maxSize;
};
public final int getMaxSize() {
return maxSize.getValue();
}
public final void setMaxSize(int value) {
maxSize.setValue(value);
}
/**
* this method is called when user inputs text into the textField
*/
@Override
public void replaceText(int start, int end, String text) {
if (numericOnly.getValue() && !text.equals("")) {
if (!text.matches("[0-9]")) {
return;
}
}
if (getText().length() < getMaxSize() || text.equals("")) {
super.replaceText(start, end, text);
}
}
/**
* this method is called when user pastes text into the textField
*/
@Override
public void replaceSelection(String text) {
if (numericOnly.getValue() && !text.equals("")) {
if (!text.matches("[0-9]+")) {
return;
}
}
super.replaceSelection(text);
if (getText().length() > getMaxSize()) {
String maxSubString = getText().substring(0, getMaxSize());
setText(maxSubString);
positionCaret(getMaxSize());
}
}
}
package com.mazeworks.cloudhms.view.components;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.IntegerPropertyBase;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.TextField;
public class NumericTextField extends TextField {
/**
* numericOnly property if set, will allow accept only numeric input.
*/
private BooleanProperty numericOnly = new SimpleBooleanProperty(this,
"numericOnly", false);
public final boolean isNumericOnly() {
return numericOnly.getValue();
}
public final void setNumericOnly(boolean value) {
numericOnly.setValue(value);
}
public final BooleanProperty numericOnlyProperty() {
return numericOnly;
}
/**
* maxSize property, determines the maximum size of the text that
can be
* input.
*/
public IntegerProperty maxSize = new IntegerPropertyBase(1000) {
@Override
public String getName() {
return "maxSize";
}
@Override
public Object getBean() {
return NumericTextField.this;
}
};
public final IntegerProperty maxSizeProperty() {
return maxSize;
}
;
public final int getMaxSize() {
return maxSize.getValue();
}
public final void setMaxSize(int value) {
maxSize.setValue(value);
}
/**
* this method is called when user inputs text into the textField
*/
@Override
public void replaceText(int start, int end, String text) {
if (numericOnly.getValue() && !text.equals("")) {
if (!text.matches("^[0-9]*\\.?[0-9]*$")) {
return;
}
}
if (getText().length() < getMaxSize() || text.equals("")) {
super.replaceText(start, end, text);
}
}
/**
* this method is called when user pastes text into the textField
*/
@Override
public void replaceSelection(String text) {
if (numericOnly.getValue() && !text.equals("")) {
if (!text.matches("^[0-9]*\\.?[0-9]*$")) {
return;
}
}
super.replaceSelection(text);
if (getText().length() > getMaxSize()) {
String maxSubString = getText().substring(0, getMaxSize());
setText(maxSubString);
positionCaret(getMaxSize());
}
}
}
@Override
public void replaceText(int start, int end, String text) {
String preText = getText(0, start);
String afterText = getText(end, getLength());
String toBeEnteredText = preText + text + afterText;
// Treat the case where the user inputs proper text and is not inputting backspaces or other control characters
// which would be represented by an empty text argument:
if (!text.isEmpty() && text.matches("\\d|\\.")) {
Logger.getAnonymousLogger().info("Paring non-empty.");
try {
Logger.getAnonymousLogger().info("Parsing " + toBeEnteredText);
Double.parseDouble(toBeEnteredText);
super.replaceText(start, end, text);
} catch (Exception ignored) {
}
}
// If the user used backspace or del, the result text is impossible to not parse as a Double/Integer so just
// enter that text right ahead:
if (text.isEmpty()) {
super.replaceText(start, end, text);
}
}
通过以下方式,可以将TextField转换为NumberField,但这种方式不会立即过滤键盘输入...但是如果您将焦点移出该字段,则会看到结果。您可以将其用于基本数字输入字段验证。我在这里提供一些示例...它可能会帮助某些人 :)
// It will show the number with comma ie. 64,568,455
TextField numberField = new TextField();
numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
// It will show the integer number ie. 64568455
TextField integerField = new TextField();
integerField.setTextFormatter(new TextFormatter<>(new IntegerStringConverter()));
// It will ensure the float format ie. 2635.0
TextField floatField = new TextField();
floatField.setTextFormatter(new TextFormatter<>(new FloatStringConverter()));