Field UF that accepts only 2 uppercase letters

3

I'm trying to create a specific field for UF, where only 2 uppercase letters will be allowed. I tried using regular expression, but I could not. Could someone help me?

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());
                }
            }
        });
    }
    
asked by anonymous 03.11.2017 / 18:51

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);
            }
        });
    }
}
    
03.11.2017 / 19:50
1

In addition to the way you are doing, you can make a ComboBox<T> for the user, being <T> an enum with all federative units available since this information do not change frequently. In this way you make it easier to manipulate the data, without having to validate the input entered by the user.

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 have to pass the enum values() array, 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 a UF ufSelecionado = ufs.getValue(); .

Result:

    
09.11.2017 / 13:24