UF field that accepts only 2 uppercase letters

Asked

Viewed 207 times

3

I am trying to create a specific field for UF, in which only 2 capital letters will be allowed. I tried to use regular expression, but I couldn’t. Could someone give me a helping hand?

public class CampoUF extends TextField {

    public CampoUF() {
        textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (!newValue.matches("\\W*")) {
                    //setText(newValue.replaceAll("[^\\W]", ""));
                    setText(newValue.toUpperCase());
                }
            }
        });
    }

2 answers

3


How about something like this?

import java.util.Locale;
import java.util.Objects;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;

public class CampoUF extends TextField {

    public CampoUF() {
        textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(
                    ObservableValue<? extends String> observable,
                    String oldValue,
                    String newValue)
            {
                if (Objects.equals(newValue, oldValue)) return;
                if (newValue.length() > 2) newValue = newValue.substring(0, 2);
                newValue = newValue.toUpperCase(Locale.ROOT);

                if (newValue.length() > 1 && (newValue.charAt(1) < 'A' || newValue.charAt(1) > 'Z')) {
                    newValue = newValue.substring(0, 1);
                }
                if (newValue.length() > 0 && (newValue.charAt(0) < 'A' || newValue.charAt(0) > 'Z')) {
                    newValue = newValue.substring(1);
                }
                setText(newValue);
            }
        });
    }
}
  • Thanks for the help ! Plus, it is not necessary to use any "regex" to block the numbers ?

  • 2

    @G1win No need to use regex. I’m already editing the answer.

  • @G1win Response edited.

  • 1

    Thank you very much !

1

In addition to the way you are doing, you can provide a ComboBox<T> for the user, being <T> an One with all federative units available as this information does not change frequently. This way you facilitate the treatment and manipulation of the data, without the need to validate the input user-typed.

Consider the Enum UF:

public enum UF {
    AC, AL, AM, AP, BA, CE, DF, ES, GO,
    MA, MG, MS, MT, PA, PB, PE, PI, PR,
    RJ, RN, RO, RR, RS, SC, SE, SP, TO;
}

In your controller, you only need to pass the array of values() of Enum, for example:

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.*;
import javafx.scene.control.ComboBox;

public final class ViewController implements Initializable {

    private @FXML ComboBox<UF> ufs;

    @Override
    public void initialize(URL url, ResourceBundle res){
        // carrega o combobox com todos os UFs.
        ufs.getItems().addAll(UF.values());
        // deixa o primeiro item selecionado.
        ufs.getSelectionModel().selectFirst();
    }
}

To get the selected UF, you only need one UF ufSelecionado = ufs.getValue();.

Upshot:

JavaFX UF Unidade Federativa

Browser other questions tagged

You are not signed in. Login or sign up in order to post.