Only chiffre dans un texfield en java

rinking -  
choubaka Messages postés 39442 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,
Je voudrais savoir comment faire pour autoriser seulement la saisie de chiffre dans un TextField (ou JTextField) en java.

Pourriez vous me donner un exemple de code.

SVP Merci.

Rinking
A voir également:

1 réponse

choubaka Messages postés 39442 Date d'inscription   Statut Modérateur Dernière intervention   2 105
 
salut

tu dois tester le String avec

public static Integer valueOf(String s)
                       throws NumberFormatExceptionReturns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string. 
In other words, this method returns an Integer object equal to the value of: 

new Integer(Integer.parseInt(s)) 

Parameters:
s - the string to be parsed. 
Returns:
an Integer object holding the value represented by the string argument. 
Throws: 
NumberFormatException - if the string cannot be parsed as an integer.


dans la gestion de l'erreur, il suffit de signaler à l'utilisateur qu'il doit recommencer correctement l'opération
0