Bonjour, je cherche à utiliser un JFormattedTextField et un maque sur un petit programme. Je me suis inspiré d'ici là et ça
Voici où j'en suis :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 public class Field extends JFormattedTextField implements FocusListener, ActionListener, MouseListener { private AbstractFormat format; public Field (AbstractFormat format) { super(format); this.format = format; this.setColumns(format.getLenth()); this.setText("");//format inits field text } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 public abstract class AbstractFormat { private MaskFormatter mask; private int lenth; public AbstractFormat(int lenth) { this.lenth = lenth; } }Mon but ici est d'avoir un champs qui ne contient que des chiffres (length au maximum). Si l'utilisateur tente d'insérer un nombre, le champs ne l'affiche pas. J'ai executé ce code. Mais rien se passe, les lettres rentrent quand même !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public class Numeric extends AbstractFormat { private int length; public Numeric(int length) { super(length); String maskString = ""; for (int i=0; i<length; i++) { maskString += "#"; } try {this.setMask(new MaskFormatter(maskString));} catch (ParseException ex) {ex.printStackTrace();} } }
Partager