【问题标题】:add listener for javafx textField upto 2 decimal place为 javafx textField 添加侦听器,最多保留小数点后 2 位
【发布时间】:2017-01-18 17:56:20
【问题描述】:

我想将 javaFX 文本字段设置为小数点后两位。我找到了答案,但它是针对数值的。例如

 // force the field to be numeric only
textField.textProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
        if (!newValue.matches("\\d*")) {
            textField.setText(newValue.replaceAll("[^\\d]", ""));
        }
    }
});

在上面的代码中,什么是限制值的替换,直到小数点后两位。 或者是否有任何其他解决方案来限制文本字段。 我有 Binding TextField 这是我的部分代码...

  @FXML public TextField InvoiceTotal;

 private DoubleProperty invTotal;
 invTotal = new SimpleDoubleProperty(0);

 netAmount.bind(grossAmount.subtract(disc));

 StringConverter<? extends Number> converter= new DoubleStringConverter();

 Bindings.bindBidirectional(InvoiceTotal.textProperty(),invTotal,(StringConverter<Number>)converter);

现在我想在 InvoiceTotal 文本字段上设置两位小数的限制

【问题讨论】:

  • 看看TextFormatter。您可以使用 UnaryOperator 定义所有内容

标签: java javafx-8 scenebuilder


【解决方案1】:

在文本字段上使用文本格式化程序。该模式只需匹配任何可能的十进制值,最多两位小数。 (类似于可选的负号,后跟任意数量的数字,然后可选地后跟一个小数点和 0-2 位数字。)如果结果文本与该模式匹配,则让文本格式化程序接受更改,否则拒绝它们。

import java.util.function.UnaryOperator;
import java.util.regex.Pattern;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class DecimalTextField extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pattern decimalPattern = Pattern.compile("-?\\d*(\\.\\d{0,2})?");

        UnaryOperator<Change> filter = c -> {
            if (decimalPattern.matcher(c.getControlNewText()).matches()) {
                return c ;
            } else {
                return null ;
            }
        };

        TextFormatter<Double> formatter = new TextFormatter<>(filter);

        TextField textField = new TextField();
        textField.setTextFormatter(formatter);
        StackPane root = new StackPane(textField);
        root.setPadding(new Insets(24));

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • 我在 TextField 上有绑定,但 UnaryOperator 对它不起作用。
  • Property listner 上是否有任何 Pattern 应用?
  • 我看不懂最后两个cmets。 Edit您的问题包括该信息。如果您的文本字段的文本绑定到某个东西,用户将无法输入它。
  • 是的,用户不允许输入...文本字段不可编辑。文本字段从绑定属性中获取值,我没有找到在绑定属性中设置小数位的任何方法。这就是为什么我要在文本字段上设置监听器。或者,如果有任何可能在绑定属性中应用小数位,那么请指导我.. 我会感谢 James_D 先生
  • @ZubairCH 同样,请edit 提供此信息的问题。如果您想要一个有用的答案,您需要实际描述您正在尝试做的事情。
【解决方案2】:

我无法抗拒。这是上面两行的答案(完成所有工作的那些)

private static TextFormatter<Double> new3DecimalFormatter(){
        Pattern decimalPattern = Pattern.compile("-?\\d*(\\.\\d{0,3})?");
        return new TextFormatter<>(c -> (decimalPattern.matcher(c.getControlNewText()).matches()) ? c : null );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多