Skip to content

Instantly share code, notes, and snippets.

@HussainDerry
Forked from drguildo/PasswordDialog
Last active November 6, 2017 11:20
Show Gist options
  • Select an option

  • Save HussainDerry/5f78df4debbe6fbf553db2d827c3cf07 to your computer and use it in GitHub Desktop.

Select an option

Save HussainDerry/5f78df4debbe6fbf553db2d827c3cf07 to your computer and use it in GitHub Desktop.
JavaFX password dialog.
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
public class PasswordDialog extends Dialog<String> {
private static final String ACCEPT_BUTTON_TEXT = "Done";
private static final String HINT = "Password";
private static final String HEADER_TEXT = "Please enter your password.";
private PasswordField passwordField;
public PasswordDialog() {
setTitle(HINT);
setHeaderText(HEADER_TEXT);
ButtonType passwordButtonType = new ButtonType(ACCEPT_BUTTON_TEXT, ButtonData.OK_DONE);
getDialogPane().getButtonTypes().addAll(passwordButtonType, ButtonType.CANCEL);
passwordField = new PasswordField();
passwordField.setPromptText(HINT);
HBox hBox = new HBox();
hBox.getChildren().add(passwordField);
hBox.setPadding(new Insets(20));
HBox.setHgrow(passwordField, Priority.ALWAYS);
getDialogPane().setContent(hBox);
Platform.runLater(() -> passwordField.requestFocus());
setResultConverter(dialogButton -> {
if (dialogButton == passwordButtonType) {
return passwordField.getText();
}
return null;
});
}
public PasswordField getPasswordField() {
return passwordField;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment