Bonjour,

Je cherche à permettre l'affichage du point décimal sous sa forme française ( , ) dans un JLabel. Je pensais donc surcharger les méthodes getText et setText de mon JLabel de cette façon :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
        public static String MARQUEUR_DECIMAL = ",";
 
        @Override
            public String getText() {
                return super.getText().replace(MARQUEUR_DECIMAL, ".");
            }
 
        @Override
            public void setText(String texte) {
                System.out.println(texte.replace(".", MARQUEUR_DECIMAL));
                super.setText(texte.replace(".", MARQUEUR_DECIMAL));
            }
Problème :
Lorsque je test mon objet, j'obtiens un résultat assez déroutant :

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
public class Main {
 
    public static void main(String[] args) {
        JFrame a=new JFrame();
        EcranDAffichage ecran = new EcranDAffichage("");
        a.getContentPane().add(ecran);
        a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        a.setSize(220,80);
        a.setVisible(true);
        ecran.setText("0,1");
    }
 
    public static String MARQUEUR_DECIMAL = ",";
 
        public static class EcranDAffichage extends JLabel {
            public EcranDAffichage(String label) {
                super(label);
                setHorizontalAlignment(JLabel.RIGHT);
                setPreferredSize(new Dimension(220, 40));
            }
        @Override
            public String getText() {
                String texte = super.getText();
                return texte.replace(MARQUEUR_DECIMAL, ".");
            }
 
        @Override
            public void setText(String texte) {
                System.out.println(texte.replace(".", MARQUEUR_DECIMAL));
                super.setText(texte.replace(".", MARQUEUR_DECIMAL));
                repaint();
            }
        }
 
}
Le println me renvoie "0,1"
Le JLabel m'affiche "0.1"...

Merci de votre aide