您好,我想创建一个javafx TextField,用户只能在当前订单中输入A11111111A [英] Hello , I want to create a javafx TextField where the user can only input in the current order A11111111A

查看:216
本文介绍了您好,我想创建一个javafx TextField,用户只能在当前订单中输入A11111111A的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以第一个和最后一个输入应该是字母,它们之间应该只是数字。这是我的代码:

So the first and last input should be letters, and between them should be only numbers. Here is my code :

tf.textProperty().addListener(new ChangeListener<String>() {
        public void changed(final ObservableValue<? extends String> ov, final String oldValue, final String newValue) {

            String text_of_first_letter = tf.getText().substring(0, 1);
            if (tf.getText().length() > 1 ) {

                if(!newValue.matches("\\d*")) {

                    tf.setText(newValue.replaceFirst("[^\\d]", ""));
                }

            }
            else if(tf.getText().length() == 1){
                System.out.println("ktu");
                tf.setText(newValue.replaceFirst("[^\\d]", text_of_first_letter));
            }
        }
    });


推荐答案

您可以使用 TextFormatter 字符串的 匹配

如果 TextField 中的文字不符合这三个正则表达式中的一个,然后显示旧文本。

If the text in the TextField does not meet one of these three Regex, then show the old text.

case 1: newVal.matches("[A-z]") -> Single alpha character
case 2: newVal.matches("[A-z]\\d+") -> Alpha character followed by digits
case 3: newVal.matches("[A-z]\\d+[A-z]") -> Alpa character followed by digits than another alpha character.




完整应用

Full app



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

/**
 *
 * @author blj0011
 */
public class JavaFXApplication149 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        TextField textField = new TextField();
        textField.setTextFormatter(new TextFormatter<>(c
                -> {
            if (c.getControlNewText().isEmpty()) {
                return c;
            }

            if (c.getControlNewText().matches("[A-z]") || c.getControlNewText().matches("[A-z]\\d+") || c.getControlNewText().matches("[A-z]\\d+[A-z]")) {
                return c;
            }
            else {
                return null;
            }

        }));



        StackPane root = new StackPane(textField);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

**更新:更改为的TextFormatter 。 @kleopatra说这是实现这一目标的正确方法。

**Update: Change to TextFormatter. @kleopatra said it's the correct way to achieve this.

这篇关于您好,我想创建一个javafx TextField,用户只能在当前订单中输入A11111111A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆