Я думаю, что это лучшее решение:
JTextField textField = new JFormattedTextField(new MaskFormatter("###")); //