过滤键盘文本字段 Java FXML

Filter Keyboard TextField Java FXML

嗯,我正在研究 Java FX,但这次我在 NetBeans 中使用 FXML,然后我想限制 TextField 允许的键。就像数字或字母一样。

我找到了 This,然后我创建了一个新的 class,然后放入该代码(在 link 中检查为正确),我扩展了 TextField,但是当我 运行 代码,抛出异常,我认为是因为SceneBuilder 没有我的Class。

更新我发现 Java FX 的类似代码:

import java.util.function.UnaryOperator;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author Alejandro
 */
public class JavaFXApplication2 extends Application {

@Override
public void start(Stage primaryStage) {

    TextField textField = new TextField();
    TextFormatter<String> textFormatter = getTextFormatter();
    textField.setTextFormatter(textFormatter);

    VBox root = new VBox();

    root.getChildren().add(textField);

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

    primaryStage.setTitle("TextFormat");
    primaryStage.setScene(scene);
    primaryStage.show();
}

 private TextFormatter<String> getTextFormatter() {
    UnaryOperator<Change> filter = getFilter();
    TextFormatter<String> textFormatter = new TextFormatter<>(filter);
    return textFormatter;
}

private UnaryOperator<Change> getFilter() {
    return change -> {
        String text = change.getText();

        if (!change.isContentChange()) {
            return change;
        }

        if (text.matches("[a-z]*") || text.isEmpty()) {
            return change;
        }

        return null;
     };
}

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

}

上面的代码在 Java FX 应用程序中工作正常,但我需要一个在 Java FXML 中使用,下面的一个人 Post 一个类似的代码,它编译,不抛出异常,但不起作用,或者我不知道如何实现。

如果您想将文本限制为 ALPHA/NUMERIC,您可以使用这样的文本格式化程序:

public class MaxLengthTextFormatter extends TextFormatter<String> {
    private int maxLength;

public MaxLengthTextFormatter(int maxLength) {
    super(new UnaryOperator<TextFormatter.Change>() {
        @Override
        public TextFormatter.Change apply(TextFormatter.Change change) {
            //If the user is deleting the text (i.e. full selection, ignore max to allow the
            //change to happen)
            if(change.isDeleted()) {
                //if the user is pasting in, then the change could be longer
                //ensure it stops at max length of the field
                if(change.getControlNewText().length() > maxLength){
                    change.setText(change.getText().substring(0, maxLength));
                }

            }else if (change.getControlText().length() + change.getText().length() >= maxLength) {
                int maxPos = maxLength - change.getControlText().length();
                change.setText(change.getText().substring(0, maxPos));
            }
            return change;
        }
    });
    this.maxLength = maxLength;
}

public int getMaxLength()
{
    return maxLength;
}

}

public class AlphaNumericTextFormatter extends TextFormatter<String> {

    /** The Constant ALPHA_NUMERIC_ONLY. */
    //private static final String ALPHA_NUMERIC_ONLY = "^[a-zA-Z0-9]*$";
    /** MAKE NUMERIC ONLY **/
    private static final String DIGITS_ONLY = "^[0-9]*$";

    /**
     * Instantiates a new alpha numeric text formatter.
     */
    public AlphaNumericTextFormatter() {
        super(applyFilter(null));
    }

    /**
     * Instantiates a new alpha numeric text formatter.
     *
     * @param maxLength
     *            the max length
     */
    public AlphaNumericTextFormatter(int maxLength) {
        super(applyFilter(new MaxLengthTextFormatter(maxLength).getFilter()));
    }

    /**
     * Apply filter.
     *
     * @param filter
     *            the filter
     * @return the unary operator
     */
    private static UnaryOperator<Change> applyFilter(UnaryOperator<Change> filter) {
        return change -> {
            if (change.getControlNewText() != null && change.getControlNewText().matches(DIGITS_ONLY)) {
                if (filter != null) {
                    filter.apply(change);
                }
                return change;
            }
            return null;
        };
    }

}

这会创建一个格式化程序,而不是只允许数字和字母 - 您可以根据需要调整模式。

你像这样将它附加到你的文本字段....

@FXML
private TextField myTextField;


@FXML
private void initialize() {
    //Create a alpha field which max length of 4
    myTextField.setTextFormatter(new AlphaNumericTextFormatter(4));
}