用于密码的 JavaFX TextInputDialog(屏蔽)

Cpt*_*mer 2 dialog javafx 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();
            }
        }
Run Code Online (Sandbox Code Playgroud)

那么是否有可能使用对话框或其他东西来屏蔽 TextInput?

Sai*_*dem 6

虽然还有其他方法可以解决这个问题,但我建议您根据您的要求实现自定义 Dialog。通过这种方式,您可以更好地控制事物。

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());
    }
}
Run Code Online (Sandbox Code Playgroud)