在输入数字时将文本中的数字删除(Javafx)

时间:2018-06-05 17:08:31

标签: java javafx delimiter

我创建了一个简单的javafx程序。当我输入textfiled中的数字时,我想将三位数分隔三位数。我使用了stackoverflow链接中给出的两个解决方案(How to format text of TextField? JavaFXJava 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number

但他们都没有为我工作。第一个解决方案(设置textformatter)对我来说是无用的(或者我可能无法以正确的方式使用它)但第二个解决方案是工作但只接受4位数,而我在文本字段中输入的其他数字是与我用逗号输入它们的风格相同。

我想像这样分别每三位数:12,564,546,554 如果有人知道解决方案,请帮助我克服这个问题。 感谢。

 import javafx.application.Application;
 import javafx.scene.Scene;
 import javafx.scene.control.TextField;
 import javafx.scene.control.TextFormatter;
 import javafx.scene.layout.HBox;
 import javafx.stage.Stage;

 import java.text.DecimalFormat;
 import java.text.ParsePosition;


 public class DelimiterExample extends Application{


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


@Override
public void start(Stage primaryStage) throws Exception {

    TextField textField = new TextField();
    HBox hBox = new HBox();

    //solution one

    DecimalFormat format = new DecimalFormat( "#,###" );

    textField.setTextFormatter( new TextFormatter<>(c ->
    {
        if ( c.getControlNewText().isEmpty() )
        {
            return c;
        }

        ParsePosition parsePosition = new ParsePosition( 0 );
        Object object = format.parse( c.getControlNewText(), parsePosition );

        if ( object == null || parsePosition.getIndex() < c.getControlNewText().length() )
        {
            return null;
        }
        else
        {

            return c;
        }

    }));


    // solution two

    textField.textProperty().addListener((obs , oldVal , newVal)-> {

        if (newVal.matches("\\d*")) {
            DecimalFormat formatter = new DecimalFormat("#,###");
            String newvalstr = formatter.format(Float.parseFloat(newVal));
            //System.out.println(newvalstr);
            textField.setText(newvalstr);
        }


    });

    hBox.getChildren().add(textField);
    Scene scene = new Scene(hBox , 100 , 100);
    primaryStage.setScene(scene);
    primaryStage.show();
}
}

2 个答案:

答案 0 :(得分:1)

任何非选择更改的修改都可以通过修改范围之前的字符来修复:

final char seperatorChar = ',';
final Pattern p = Pattern.compile("[0-9" + seperatorChar + "]*");
textField.setTextFormatter(new TextFormatter<>(c -> {
    if (!c.isContentChange()) {
        return c; // no need for modification, if only the selection changes
    }
    String newText = c.getControlNewText();
    if (newText.isEmpty()) {
        return c;
    }
    if (!p.matcher(newText).matches()) {
        return null; // invalid change
    }

    // invert everything before the range
    int suffixCount = c.getControlText().length() - c.getRangeEnd();
    int digits = suffixCount - suffixCount / 4;
    StringBuilder sb = new StringBuilder();

    // insert seperator just before caret, if necessary
    if (digits % 3 == 0 && digits > 0 && suffixCount % 4 != 0) {
        sb.append(seperatorChar);
    }

    // add the rest of the digits in reversed order
    for (int i = c.getRangeStart() + c.getText().length() - 1; i >= 0; i--) {
        char letter = newText.charAt(i);
        if (Character.isDigit(letter)) {
            sb.append(letter);
            digits++;
            if (digits % 3 == 0) {
                sb.append(seperatorChar);
            }
        }
    }

    // remove seperator char, if added as last char
    if (digits % 3 == 0) {
        sb.deleteCharAt(sb.length() - 1);
    }
    sb.reverse();
    int length = sb.length();

    // replace with modified text
    c.setRange(0, c.getRangeEnd());
    c.setText(sb.toString());
    c.setCaretPosition(length);
    c.setAnchor(length);

    return c;
}));

答案 1 :(得分:0)

再次感谢@ fabian。我在这个问题上坚持了两天。我还发现我可以使用此代码获得我想要的货币风格。现在我有两个完美的解决方案。

      textField.setOnKeyTyped(event -> {
        String typedCharacter = event.getCharacter();
        event.consume();

        if (typedCharacter.matches("\\d*")) {
            String currentText = 
            textField.getText().replaceAll("\\.","").replace(",", "");
            long longVal = Long.parseLong(currentText.concat(typedCharacter));
            textField.setText(new DecimalFormat("#,##0").format(longVal));
        }
    });