如何在JavaFX 2.0中实现一个数字输入框?

21

我需要在我的用户界面中插入一个数字字段。因此,我需要检查文本字段上的关键事件,以检查输入字符是否为数字。我通过扩展TextField创建了一个类。如果TextField类中有一个处理keyEvents的方法,我可以简单地重写该方法以适用于数字字段。有任何想法吗?

谢谢


2
请将您的解决方案作为答案发布并标记为已接受! - pmoule
8个回答

15

2016年5月27日更新

Java 8u40引入了TextFormatter类,这是推荐的实现此功能的方法(尽管本答案提供的解决方案仍然有效)。有关更多信息,请参见Uwe's answerHassan's answer以及其他提到以下问题的答案中使用TextFormatter:

还有另一个答案中提供的解决方案,我没有尝试过,但看起来很好,被StackOverflow的一位管理员删除了:

TextField numberField = new TextField();
numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));

上面的代码缺少TextFormatter的UnaryOperator过滤器,通常也需要它(否则,该字段不会显示将用户输入限制为仅格式化的值,而只允许您通过文本格式化程序的value属性来监视未格式化的值)。要扩展解决方案以使用过滤器,可以使用下面的代码:
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,以便进行简单的绑定。备用解决方案类似于原始帖子中更新的问题概述(即添加事件过滤器以限制来自键事件的输入数据),但另外还添加了一个ChangeListener在TextField的文本属性上,以确保只有数字才能被接受复制和粘贴的值。

在JavaFX论坛线程Numeric Textfield in JavaFX 2.0?中还有其他一些解决此问题的解决方案,其中包括对number fields from FXExperience controls的引用。


1
这应该作为JavaFX组件提供。 - pdem
1
Java 8u40引入了TextFormatter,它提供了一种通用的方法来获取这种功能,尽管它的文档不是很完善,并且它不仅限于数字字段。但是,如果在JavaFX库核心中有一个开箱即用的组件提供这样的功能(现在已经有TextFormatter了),或者在第三方扩展库(如ControlsFX)中有这样的组件,那就太好了。 - jewelsea
@jewelsea,“NumberStringConverter”对我没用。我建议尝试一下。 - Kachna
@Kachna 感谢提供的信息,这有点棘手。最初呈现的NumberStringConverter仅用作格式化程序而不是过滤器,因此它可以正确提取输出值,但不会更改输入值的显示或限制用户可以键入的内容(这是大多数人想要的),因此我更新了答案,包括使用TextFormatter和NumberStringConverter的过滤器示例。 - jewelsea

3

FXExperience上有一个提示,与此类似的问题都可以解决。

简单说,您需要扩展TextField并覆盖replaceText()replaceSelection()方法,过滤掉所有不是数字的输入。

一旦实现,这两种方法应遵循以下模板:

if (!newText.matches("[0-9]")) {
    super.call(allParams)
}

然而,我也确认TextField无法避免粘贴限制的值到其中,正如教程中所讨论的那样。 - Uluk Biy

3
找到了解决方案。 :)
public 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();
            }
         }
      });
   }
}

2

针对多个包括小数点在内的文本框进行操作

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)
            ));

2

这是我编写的 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());
    }
}

}


1

结合 BaiJiFeiLong 和 AJAY PRAKASH 解决方案以支持小数输入

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());
        }
    }
}

0
继承自TextField并覆盖replaceText,以获取仅包含Double值的TextField:
@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);
    }
}

0

通过以下方式,可以将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()));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接