Contôle de saisie textfieled (java)

Fermé
asma assouma Messages postés 43 Date d'inscription vendredi 30 janvier 2009 Statut Membre Dernière intervention 22 avril 2010 - 30 mars 2010 à 13:04
UaLShark Messages postés 191 Date d'inscription vendredi 19 mai 2006 Statut Membre Dernière intervention 21 juillet 2010 - 30 mars 2010 à 13:39
Bonjour à tous, je travail sur netbeans je besoin de faire un contrôle de saisie sur une zone textfieled n'accepte que de nombre

1 réponse

UaLShark Messages postés 191 Date d'inscription vendredi 19 mai 2006 Statut Membre Dernière intervention 21 juillet 2010 35
30 mars 2010 à 13:39
Bonjour,
Vous pouvez utiliser la classe InputVerifier de cette maniere:

public class NumericVerifier extends InputVerifier
{....
.....
@Override
public boolean verify(JComponent srcComponent)
{
JTextField tmpField = (JTextField)srcComponent;
boolean inputOk ;
if(tmpField.getText().length()==0)
return true;
String txt = this.cleanText(tmpField.getText());
if(! (inputOk = checkNumericText(txt)))
return false;
this.formatText(txt);
return (inputOk && this.checkCustomConstraints(txt));
}
....
....
private boolean checkNumericText(String txt)
{
int nbDecimalDot = 0;
for(int i=0;i<txt.length();i++)
{
char c= txt.charAt(i);
if((c<'0') || (c>'9'))
{
if(c=='.')
{
if(this.fixedDecimalDigits==0)
return false;
nbDecimalDot++;
if (nbDecimalDot>1)
return false;
}
else
{
if((c=='-' || c=='+'))
{
if(i != 0)
return false;
}
else
return false;
}

}
}
return true;
}
....
//Definissez ici vos contraintes specifiques
private boolean checkCustomConstraints(String txt)
{
.....
}
}

Finalement
Le Jtextfield en question :
JTextField.setInputVerifier(new NumericVerifier ());
0