для наиболее безопасного обхода вы должны реализовать DocumentListener

import java.awt.Dimension;
import java.text.ParseException;
import javax.swing.*;
import javax.swing.text.*;
public class FormatPhone {
private static void createAndShowUI() {
JPanel panel = new JPanel();
JFormattedTextField telefoonnummer = new JFormattedTextField(createFormatter("###/######"));
Dimension teleSize = telefoonnummer.getPreferredSize();
telefoonnummer.setPreferredSize(new Dimension(100, teleSize.height));
JFormattedTextField telephoneNumberA = new JFormattedTextField(new JFormattedTextField.AbstractFormatter() {
private static final int MAX_LENGTH = 9;
private MaskFormatter smallFormat = createFormatter("##/######");
private MaskFormatter bigFormat = createFormatter("###/######");
private static final long serialVersionUID = 1L;
@Override
public Object stringToValue(String text) throws ParseException {
String simpleText = text.replaceAll("/", "").replaceAll("\\.", "");
if (simpleText.length() < MAX_LENGTH) {
return smallFormat.stringToValue(text);
} else {
return bigFormat.stringToValue(text);
}
}
@Override
public String valueToString(Object value) throws ParseException {
if (value != null) {
String valueText = (String) value;
System.out.println(valueText.length());
}
return smallFormat.valueToString(value);
}
});
telephoneNumberA.setPreferredSize(telefoonnummer.getPreferredSize());
JFormattedTextField telephoneNumberB = new JFormattedTextField(new JFormattedTextField.AbstractFormatter() {
private static final int MAX_LENGTH = 9;
private static final long serialVersionUID = 1L;
@Override
public Object stringToValue(String text) throws ParseException {
return text.replaceAll("/", "");
}
@Override
public String valueToString(Object value) throws ParseException {
if (value == null) {
return null;
}
String valueText = (String) value;
if (!valueText.matches("\\d+")) {
return null;
}
if (valueText.length() < MAX_LENGTH - 1) { // < 8
return null;
}
if (valueText.length() == MAX_LENGTH - 1) { // == 8
return valueText.substring(0, 2) + "/" + valueText.substring(2);
} else if (valueText.length() >= MAX_LENGTH) { // >= 9
valueText = valueText.substring(0, 9);
return valueText.substring(0, 3) + "/" + valueText.substring(3);
} else {
return null;
}
}
@Override
protected DocumentFilter getDocumentFilter() {
return new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String text,
AttributeSet attr) throws BadLocationException {
if (!text.matches("\\d+")) {
return;
}
String fbText = fb.getDocument().getText(0, fb.getDocument().getLength()).replaceAll("/", "");
if (fbText.length() + text.length() > MAX_LENGTH) {
return;
}
super.insertString(fb, offset, text, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
if (!text.matches("\\d+")) {
return;
}
String fbText = fb.getDocument().getText(0, fb.getDocument().getLength()).replaceAll("/", "");
if (fbText.length() + text.length() - length > MAX_LENGTH) {
return;
}
super.replace(fb, offset, length, text, attrs);
}
};
}
});
telephoneNumberB.setPreferredSize(telefoonnummer.getPreferredSize());
panel.add(telefoonnummer);
panel.add(telephoneNumberA);
panel.add(telephoneNumberB);
JFrame frame = new JFrame("Format Phone Number");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
static MaskFormatter createFormatter(String format) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(format);
formatter.setPlaceholderCharacter('.');
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowUI();
}
});
}
private FormatPhone() {
}
}