密码的JavaFX TextInputDialog(屏蔽)

时间:2018-12-18 01:50:21

标签: javafx dialog masking

我没有找到解决问题的简单方法。我想使用TextInputDialog,在其中您必须输入用户密码,以重置数据库中的所有数据。 TextInputDialog的问题在于它没有掩盖文本,而且我不知道执行此操作的任何选项。

我的代码:

public void buttonReset() {
        TextInputDialog dialog = new TextInputDialog("Test");
        dialog.setTitle("Alle Daten löschen");
        dialog.setHeaderText("Sind Sie sich ganz sicher? Damit werden alle im Programm vorhandenen Daten gelöscht.");
        dialog.setContentText("Bitte geben Sie zur Bestätigung ihr Passwort ein:");
        Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image("/icons8-blockchain-technology-64.png"));

        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()){
            try {
                if (connector.checkUserPassword(userName, result.get())) {
                    System.out.println("Your name: " + result.get());
                } else {
                    exc.alertWrongPassword();
                    buttonReset();
                }
            } catch (TimeoutException te) {
                te.printStackTrace();
                exc.alertServerNotReached();
            }
        }

那么对话或掩盖TextInput的可能性是否存在?

1 个答案:

答案 0 :(得分:0)

尽管还有其他方法可以解决此问题,但我还是建议您根据需要实现自定义对话框。这样,您可以更好地控制事物。

public void buttonReset() {
    Dialog<String> dialog = new Dialog<>();
    dialog.setTitle("Alle Daten löschen");
    dialog.setHeaderText("Sind Sie sich ganz sicher? Damit werden alle im Programm vorhandenen Daten gelöscht.");
    dialog.setGraphic(new Circle(15, Color.RED)); // Custom graphic
    dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);

    PasswordField pwd = new PasswordField();
    HBox content = new HBox();
    content.setAlignment(Pos.CENTER_LEFT);
    content.setSpacing(10);
    content.getChildren().addAll(new Label("Bitte geben Sie zur Bestätigung ihr Passwort ein:"), pwd);
    dialog.getDialogPane().setContent(content);
    dialog.setResultConverter(dialogButton -> {
        if (dialogButton == ButtonType.OK) {
            return pwd.getText();
        }
        return null;
    });

    Optional<String> result = dialog.showAndWait();
    if (result.isPresent()) {
        System.out.println(result.get());
    }
}
相关问题