1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
/**
*
* JTextField autorisant uniquement la saisie de nombres positifs.
*/
public class myTextField extends JTextField
{
public myTextField()
{
super();
}
@Override
protected Document createDefaultModel()
{
return new FileCaseDocument();
}
static class FileCaseDocument extends PlainDocument
{
String text = null;
String str1, str2;
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
{
if (str == null)
{
return;
}
text = this.getText(0, this.getLength());
str1 = text.substring(0, offs);
str2 = text.substring(offs, this.getLength());
text = str1+str+str2;
if (text.matches( "(0)|([1-9][0-9]*)" ))
{
super.insertString(offs, str, a);
}
}
}
} |
Partager