Java Swing Text Format getRegexFormatter(String regex)

Here you can find the source of getRegexFormatter(String regex)

Description

get Regex Formatter

License

Open Source License

Declaration

public static DefaultFormatter getRegexFormatter(String regex) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.DefaultFormatter;

public class Main {
    public static DefaultFormatter getRegexFormatter(String regex) {
        final Pattern pattern = Pattern.compile(regex);

        DefaultFormatter formatter = new DefaultFormatter() {

            @Override//  w  w w .  jav a  2s  .  c  o  m
            public Object stringToValue(String text) throws ParseException {
                Matcher matcher = pattern.matcher(text);

                if (matcher.matches())
                    return text;

                throw new ParseException("Pattern did not match", 0);
            }

        };

        return formatter;
    }
}

Related

  1. formatCpfCNPJ(String str)
  2. formatNumericString(String string, String mask)
  3. getBidiVisualPosition(final int start, final int end, final Element neighbouringElement, final int pos, final Position.Bias b0, final Position.Bias[] biasRet, final int length, final boolean toWest, final boolean isLTR)
  4. getElementByPosition(final Element rootElement, final int pos)
  5. getElementIndex(Element elem)
  6. getRowIndex(Element cell)
  7. getTrivialVisualPosition(final boolean toWest, final int pos, final Position.Bias bias, final int docLength, final Position.Bias[] biasRet, final boolean isLTR)