0

I created a simple javafx application that implements masking for TextField but all the sudden I encountered an unfamiliar error, below is the code:

Main.java

package com.example;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import jidefx.scene.control.field.MaskTextField;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        MaskTextField field = new MaskTextField();

        VBox root = new VBox(20);
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(field);

        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show(); 
    }  

}

MaskTextField.java

package com.example;

import java.util.ArrayList;
import javafx.scene.control.TextField;

public class MaskTextField extends TextField {

    private String mask;
    private ArrayList<String> patterns;

    public MaskTextField() {
        super();
        patterns = new ArrayList<String>();
    }

    public MaskTextField(String text) {
        super(text);
        patterns = new ArrayList<String>();
    }

    @Override
    public void replaceText(int start, int end, String text) {


        String tempText = this.getText() + text;

        if(mask == null || mask.length() == 0){
            super.replaceText(start, end, text);
        }else if (tempText.matches(this.mask) || tempText.length() == 0) {        //text.length == 0 representa o delete ou backspace

            super.replaceText(start, end, text);

        } else {

            String tempP = "^";

            for (String patt : this.patterns) {

                tempP += patt;

                if (tempText.matches(tempP)) {

                    super.replaceText(start, end, text);
                    break;

                }

            }

        }

    }

    /**
     * @return the Regex Mask
     */
    public String getMask() {
        return mask;
    }

    /**
     * @param mask the mask to set
     */
    public void setMask(String mask) {

        String tempMask = "^";

        for (int i = 0; i < mask.length(); ++i) {

            char temp = mask.charAt(i);
            String regex;
            int counter = 1;
            int step = 0;

            if (i < mask.length() - 1) {
                for (int j = i + 1; j < mask.length(); ++j) {
                    if (temp == mask.charAt(j)) {
                        ++counter;
                        step = j;
                    } else if (mask.charAt(j) == '!') {
                        counter = -1;
                        step = j;
                    } else {
                        break;
                    }
                }
            }
            switch (temp) {

                case '*':
                    regex = ".";
                    break;
                case 'S':
                    regex = "[^\\s]";
                    break;
                case 'P':
                    regex = "[A-z.]";
                    break;
                case 'M':
                    regex = "[0-z.]";
                    break;
                case 'A':
                    regex = "[0-z]";
                    break;
                case 'N':
                    regex = "[0-9]";
                    break;
                case 'L':
                    regex = "[A-z]";
                    break;
                case 'U':
                    regex = "[A-Z]";
                    break;
                case 'l':
                    regex = "[a-z]";
                    break;
                case '.':
                    regex = "\\.";
                    break;
                default:
                    regex = Character.toString(temp);
                    break;

            }

            if (counter != 1) {

                this.patterns.add(regex);

                if (counter == -1) {
                    regex += "+";
                    this.patterns.add(regex);
                } else {
                    String tempRegex = regex;
                    for (int k = 1; k < counter; ++k) {
                        regex += tempRegex;
                        this.patterns.add(tempRegex);
                    }
                }

                i = step;

            } else {
                this.patterns.add(regex);
            }

            tempMask += regex;

        }

        this.mask = tempMask + "$";

    }

}

After executing the code above, the application will throw an error:

Caused by: java.lang.NoSuchMethodError: java.lang.invoke.LambdaMetafactory.metaFactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
    at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
    at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:962)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:987)
    at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1390)
    at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1746)
    at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:477)
    ... 13 more
Java Result: 1

How to fix this one?

Walker
  • 307
  • 4
  • 15
  • 1
    The import in your `Main` class doesn't match the package name in the `MaskTextField` class you showed. Is that an error in the question, or an error in your code? – James_D Feb 05 '16 at 02:41
  • Yeah, you're right! That solved the problem! Thanks! – Walker Feb 05 '16 at 03:24
  • 1
    As an aside, you may wish to make use of a [TextFormatter](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextFormatter.html) for your text field masking implementation. Similar to [this example](http://stackoverflow.com/questions/35093145/string-with-numbers-and-letters-to-double-javafx/35096648#35096648), though obviously your functionality is regexp based so it would be more complicated than the linked example. In particular the change filter implementation could be more complicated. Just highlighting this as an alternate solution, not necessarily better. – jewelsea Feb 05 '16 at 21:04

0 Answers0