Java – AddUser – RegisterJDialog
Posted on: 23 novembre 2021 /
Categories:
public class RegisterJDialog extends JDialog implements ActionListener, PropertyChangeListener, WindowListener{
private static final long serialVersionUID = 1L;
private JLabel nameLabel;
private JLabel emailLabel;
private JLabel passwordLabel;
private JTextField nameField;
private JTextField emailField;
private JPasswordField passwordField ;
private JButton boutonRegister;
private ConnectionRest connectionRest;
private PropertyChangeSupport ptChSupport = new PropertyChangeSupport(this);
public RegisterJDialog(JFrame mainFrame){
super(mainFrame);
initialize();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(boutonRegister)) {
try {
JSONObject jsonAuthentification = new JSONObject();
jsonAuthentification.put("name",nameField.getText());
jsonAuthentification.put("email",emailField.getText());
jsonAuthentification.put("password",new String(passwordField.getPassword()));
jsonAuthentification.put("licence","MNA-1A-5U-1");
connectionRest = new ConnectionRest("CREATE_USER");
connectionRest.setObj(jsonAuthentification);
connectionRest.addPropertyChangeListener(this);
connectionRest.start();
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if(evt.getSource().equals(connectionRest)){
if(((String)evt.getNewValue()).charAt(0)=='{'){
System.err.println("Erreur : "+evt.getNewValue());
}else {
ptChSupport.firePropertyChange("AuthorizationOk", null, evt.getNewValue());
this.setVisible(false);
}
}
}
private void initialize() {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
this.setSize(240, 190);
this.setLocationRelativeTo(null);
this.addWindowListener(this);
this.setTitle("Enregistrement");
nameLabel = new JLabel("Nom : ");
emailLabel = new JLabel("E-mail : ");
passwordLabel = new JLabel("Password : ");
nameField = new JTextField();
emailField = new JTextField();
passwordField = new JPasswordField();
boutonRegister = new JButton("S'enregistrer");
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0;
getContentPane().add(nameLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
getContentPane().add(nameField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0;
getContentPane().add(emailLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
getContentPane().add(emailField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
getContentPane().add(passwordLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
getContentPane().add(passwordField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.weightx = 2;
gbc.gridwidth = 2;
getContentPane().add(boutonRegister, gbc);
boutonRegister.addActionListener(this);
} catch (Exception exc) {
exc.printStackTrace();
}
}
public void windowClosing(WindowEvent e) {
ptChSupport.firePropertyChange("Login", null,null);
this.setVisible(false);
}
public void addPropertyChangeListener(PropertyChangeListener l){ptChSupport.addPropertyChangeListener(l);}
public void removePropertyChangeListener(PropertyChangeListener l){ptChSupport.removePropertyChangeListener(l);}
public void windowDeactivated(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}