Java Change Text by asterisks in JOptionPane

0

Good day.

I have the following code that asks for a password but I want them to be seen in asterisks instead of what I write.

@FXML
public void pedirPass() {
    String pass = "root";
    String dialogpass;
    try {
        dialogpass = JOptionPane.showInputDialog("Contraseña Administrador:");

        if (dialogpass.isEmpty()) {
            Alert alert = new Alert(AlertType.WARNING);
            alert.setTitle("Pass");
            alert.setHeaderText("Error");
            alert.setContentText("No has escrito nada!");

            alert.showAndWait();
        } else if (dialogpass.equals(pass)) {
            System.out.println("Entraste");

        } else {
            dialogpass = JOptionPane.showInputDialog(null, "Escriba nuevamente la contraseña", "Error!", JOptionPane.ERROR_MESSAGE);
            if (dialogpass.equals(pass)) {
                txt_KmIni.setDisable(false);
            } else {
                Alert alert = new Alert(AlertType.WARNING);
                alert.setTitle("Control Vehicular");
                alert.setHeaderText("ATENCION, Consulte con el Administrador");
                alert.setContentText("Vulva a intentarlo con la contraseña correcta!");

                alert.showAndWait();
            }
        }

    } catch (Exception e) {

    }
}
    
asked by Riddick 17.11.2017 в 18:02
source

1 answer

1

Hello Riddick!
The showInputDialog () method of the JOptionPane class allows you to create dialogs for data entry easily, however, to do what you want is to necessary to create a customizable dialog, you could easily do so by adding a JPasswordField instead of the text string and then getting the array of entered characters to proceed with the password check.

To get the characters written in JPasswordField , you can use:

new String(contraseña.getPassword());

For example:

JPasswordField contraseña = new JPasswordField();
if(JOptionPane.showConfirmDialog(null, contraseña, "Ingrese contraseña", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION){
    dialogpass = new String(contraseña.getPassword());
    // Haces la comprobación de la contraseña
}
    
answered by 17.11.2017 / 18:33
source