Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply mask formatting to TextField?

Tags:

javafx-2

I am creating some forms and I need to create masks and validation for some fields.

Is it implemented in anyway in JavaFX?

like image 724
ftkg Avatar asked Dec 05 '12 20:12

ftkg


2 Answers

My example of the mask.

Using:

<MaskField mask="+7(DDD)DDD-DDDD"/>
<MaskField mask="AA DDD AAA" placeholder="__ ### ___"/>

etc

like image 189
vas7n Avatar answered Oct 02 '22 14:10

vas7n


Restricting input from Richard's fxexperience post:

TextField field = new TextField() {
    @Override public void replaceText(int start, int end, String text) {
        // If the replaced text would end up being invalid, then simply
        // ignore this call!
        if (!text.matches("[a-z]")) {
            super.replaceText(start, end, text);
        }
    }

    @Override public void replaceSelection(String text) {
        if (!text.matches("[a-z]")) {
            super.replaceSelection(text);
        }
    }
};

If you want to create your use a mask and create your own control, take a look at Richard's MoneyField, which also includes a sample project and source. Along the same lines there are controls to restict input to Integers, Doubles or formatted web colors (e.g. #rrggbb) in the fxexperience repository. All of these follow a common theme where they subclass Control, provide some properties to be get and set which define the public interface and then also define a private backing skin which handles rendering of the UI based on the values set through the public interface.

like image 20
jewelsea Avatar answered Oct 02 '22 14:10

jewelsea