|
|
|
|
Bonjour,
Voici mon probleme : j'ai un JTexField, et je voudrais que quand l'utilisateur appuie sur une touche autre qu'un chiffre (0,1,2,3,4,5,6,7,8,9,0) le caractère correspondant a cette mauvaise touche appuyer soit ignorer et non rentrer dans le JTextField (Sans afficher de message d'erreur à l'utilisateur).
Ceci en langage Java.
Pourriez vous me completer mon exemple, svp merci.
import javax.swing.*;
public class Toto{
public Toto(){
JFrame jf = new Frame("Toto");
JTextField jtf = new JTextField(20);
jf.add(jtf);
jf.setVisible(true);
}
public static void main (String[] args){
new Toto();
}
}
Salut
|
Petit exemple ici : import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;
public class TestFormatage extends JFrame{
public TestFormatage(){
super();
JFormattedTextField jtf = new JFormattedTextField(createFormatter("######"));
jtf.setColumns(10);
JPanel contentPane = (JPanel)getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(jtf);
setSize(300, 400);
setVisible(true);
}
private MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}
public static void main(String args[]){
new TestFormatage();
}
}Cet exemple permet de ne saisir que 6 chiffres.
Bonne continuation ;-) -- Alex pour vous servir -- -- N'oubliez pas de mettre vos sujets en "Résolu" lorsque vous avez la réponse ;-) --
|