简体   繁体   English

如何动态地将事件处理程序从文本字段中的数字仅更改为字母?

[英]How dynamically changing the event handler from numbers only to letters in textfield?

I have a ComboBox and a Textfield , I need that when I select 'letters' in the ComboBox the Textfield accepts only letters and when I select 'numbers' in the ComboBox the Textfield accepts only numbers我有一个ComboBox和一个Textfield ,当我在ComboBox选择“字母”时, Textfield只接受字母,当我在ComboBox选择“数字”时, Textfield只接受数字

JDK jdk1.8.0_201: JDK jdk1.8.0_201:

.
.
.
        oneComboBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> ov, String preview, String last) {
                    if(last.equals("Letter")) {
                        codeTextField.textProperty().addListener(new ChangeListener<String>() {
                            @Override
                            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                                if (!newValue.matches("-?([0-9]*)?")) {
                                    codeTextField.setText(oldValue);
                                }
                            }
                        });
                    }else if(last.equals("Number")) {
                        codeTextField.textProperty().addListener(new ChangeListener<String>() {
                            @Override
                            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                                if (!newValue.matches("\\sa-zA-Z*")) {
                                       codeTextField.setText(newValue.replaceAll("[^\\sa-zA-Z]", ""));
                                    }
                            }
                        });
                    }
                }
            });
.
.
.

You should create two TextFormatters and set them based on the ComboBox selection.您应该创建两个TextFormatters并根据ComboBox选择设置它们。 MCVE: MCVE:

import java.util.function.UnaryOperator;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

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

    @Override
    public void start(Stage primaryStage)
    {
        ObservableList<String> options = FXCollections.observableArrayList(
                "A",
                "2",
                "3",
                "b"
        );

        TextField textField = new TextField();
        ComboBox<String> comboBox = new ComboBox<>(options);

        UnaryOperator<TextFormatter.Change> numbersOnly = (TextFormatter.Change change) -> {
            System.out.println(change);
            String text = change.getText();
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    return null;
                }
            }

            return change;
        };

        UnaryOperator<TextFormatter.Change> characterOnly = (TextFormatter.Change change) -> {
            System.out.println(change);
            String text = change.getText();
            for (int i = 0; i < text.length(); i++) {
                if (Character.isDigit(text.charAt(i))) {
                    return null;
                }
            }

            return change;
        };

        comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (Character.isDigit(newValue.charAt(0))) {
                textField.clear();
                textField.setTextFormatter(new TextFormatter(numbersOnly));
            }
            else {
                textField.clear();
                textField.setTextFormatter(new TextFormatter(characterOnly));
            }
        });

        VBox root = new VBox();
        root.getChildren().addAll(comboBox, 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);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询仅从表中返回数字或数字+字母组合。 如何修改它以显示带字母的记录? - Query is returning only numbers or numbers+letters combination from table. How to modify it to show records with letters? Java-如何在不使用!@#$%^&*()的情况下从0打印到z?&gt; &lt;“:{} |字母,仅包括字母和数字 - Java - How to print from 0 to z without !@#$%^&*()?><":{}| letters, only alphabet and numbers 如何计算唯一字符(仅字母和数字) - How to count unique characters (only letters and numbers) 如何检查字符串是否仅由字母和数字组成 - How to check if a string is made only of letters and numbers 如何使用JFormattedTextField只允许字母和数字 - How to use JFormattedTextField allowing only letters and numbers JavaFX:如何使用事件处理程序处理textField上的文本选择? - JavaFX: How to handle text selection on textField with event handler? 在Java中,如何检查字符串是否同时包含字母和数字,但仅包含字母和数字? - In Java, how do I check if a string consists of both letters and numbers, but only letters and numbers? java - 如何从包含数字和字母的序列中添加数字? - How to add numbers from a sequence that contains numbers and letters in java? 我怎样才能创建一个只接受字母退格和空格键的文本字段 - how can i make a textfield that only accepts letters backspace and spacebar only 正则表达式仅匹配字母和数字 - Regex to match only letters and numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM