用于密码的 JavaFX TextInputDialog(屏蔽)

JavaFX TextInputDialog for Password (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?

虽然可以有其他方法来解决这个问题,但我建议根据您的要求实现自定义对话框。这样你就可以更好地控制事情。

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());
    }
}