Empty JTextField ?
Hello,
I have a weird problem that I haven't solve. In my program, I would
like print in the terminal the value of the JTextField fieldNom when I
click on the button "Lancer la modification". But it always print an
empty string...
Here's the code :
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Fenetre extends JFrame implements ActionListener {
private Container container;
private JLabel labelNom;
private JLabel labelVieuxMdp;
private JLabel labelNouveauMdp;
private JLabel labelConfirmerMdp;
private JTextField fieldNom;
private JPasswordField fieldVieuxMdp;
private JPasswordField fieldNouveauMdp;
private JPasswordField fieldConfirmerMdp;
private JButton buttonLancer;
public Fenetre() {
super();
setTitle("Changer mot de passe");
setSize(350,150);
setLocation(200,200);
setContentPane(construireFenetre());
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Fenetre f = new Fenetre();
f.construireFenetre();
}
});
}
public void actionPerformed(ActionEvent e) {
System.out.println("nom : "+getNom());
}
private Container construireFenetre() {
container = new JPanel();
container.setLayout(new GridBagLayout());
GridBagConstraints contraintes = new GridBagConstraints();
labelNom = new JLabel();
labelNom.setText("Nom");
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.5;
contraintes.gridx = 0;
contraintes.gridy = 0;
container.add(labelNom, contraintes);
fieldNom = new JTextField(25);
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.5;
contraintes.gridx = 1;
contraintes.gridy = 0;
container.add(fieldNom, contraintes);
labelVieuxMdp = new JLabel();
labelVieuxMdp.setText("Mot de passe");
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.5;
contraintes.gridx = 0;
contraintes.gridy = 1;
container.add(labelVieuxMdp, contraintes);
fieldVieuxMdp = new JPasswordField();
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.5;
contraintes.gridx = 1;
contraintes.gridy = 1;
container.add(fieldVieuxMdp, contraintes);
labelNouveauMdp = new JLabel();
labelNouveauMdp.setText("Nouveau mot de passe");
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.5;
contraintes.gridx = 0;
contraintes.gridy = 2;
container.add(labelNouveauMdp, contraintes);
fieldNouveauMdp = new JPasswordField();
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.5;
contraintes.gridx = 1;
contraintes.gridy = 2;
container.add(fieldNouveauMdp, contraintes);
labelConfirmerMdp = new JLabel();
labelConfirmerMdp.setText("Retapez le mot de passe");
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.5;
contraintes.gridx = 0;
contraintes.gridy = 3;
container.add(labelConfirmerMdp, contraintes);
fieldConfirmerMdp = new JPasswordField();
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.5;
contraintes.gridx = 1;
contraintes.gridy = 3;
container.add(fieldConfirmerMdp, contraintes);
buttonLancer = new JButton();
buttonLancer.setText("Lancer la modification");
contraintes.fill = GridBagConstraints.HORIZONTAL;
contraintes.weightx = 0.0;
contraintes.gridwidth = 2;
contraintes.gridx = 0;
contraintes.gridy = 4;
buttonLancer.addActionListener(this);
container.add(buttonLancer, contraintes);
return container;
}
public String getNom() {
return fieldNom.getText();
}
public String getVieuxMdp() {
return new String(fieldVieuxMdp.getPassword());
}
public String getNouveauMdp() {
return new String(fieldNouveauMdp.getPassword());
}
public String getConfirmerMdp() {
return new String(fieldConfirmerMdp.getPassword());
}
}
Thanks,
Vincent