Here you can find the source of getRegexFormatter(String regex)
public static DefaultFormatter getRegexFormatter(String regex)
//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; } }