Bonjour,
Mon JFormattedTextField me permet de saisir une date au format "dd/MM/yyyy".
Je souhaite vérifier si la saisie est conforme au format.
Voici le code de ma classe :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
47
48
49
50
51
52
53
54
55 import java.awt.Font; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JOptionPane; public class SaisieDate extends JFormattedTextField implements FocusListener{ private FenetrePrincipale fenetrePrincipale; SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy"); public SaisieDate(SimpleDateFormat formatDate){ this.setFont(new Font("SANS SERIF", Font.BOLD, 16)); this.setText(""); this.setBounds(45, 140, 120, 30); this.setHorizontalAlignment(JFormattedTextField.CENTER); this.addFocusListener(this); } @Override public void focusGained (FocusEvent e){ this.setText(""); } @Override public void focusLost (FocusEvent e){ String dateEntree = getDate(); if(! (dateEntree).equals("")){ try{ Date date = formatDate.parse(dateEntree); this.setText(date.toString()); } catch(ParseException exception){ JOptionPane.showMessageDialog(fenetrePrincipale, "Mauvais format pour la date !", "Attention", JOptionPane.ERROR_MESSAGE); } } } public String getDate(){ return this.getText(); } }
Le problème est que la date s'affiche sous le format "wed JUNE 25...".
De plus si on tape par exemple "24415/151456/214521" le message d'erreur n'est pas renvoyé.
Partager