简体   繁体   中英

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

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. 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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM