import javax.swing.JTextField;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.AttributeSet;
import javax.swing.JFrame;
import javax.swing.text.BadLocationException;
public class Main extends JFrame{
public Main() {
JTextField field = new JTextField();
field.setColumns(1);
field.setDocument(new Main.Customdoc(1));
getContentPane().add(field);
setSize(50,50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class Customdoc extends DefaultStyledDocument{
private String mask = "0123456789";
private int maxLength = 0;
public Customdoc(int max){
this.maxLength = max;
}
public void insertString(int off, String str, AttributeSet a) throws BadLocationException{
if(str.length()+getLength() > maxLength)
return;
if(mask.indexOf(str)==-1)
return;
super.insertString(off,str,a);
}
}
public static void main(String[] args){
Main m = new Main();
m.setVisible(true);
}
}