i have a custom login dialog box largely copied from here it works fine, but i am facing problem with getting it to show and get userdata at most 3 times before allowing the user forward if credentials are true or closing the program if wrong credentials are entered 3 times in a row. Heres my code....
//**************************login form*********************************
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login");
dialog.setHeaderText("Welcome to MHI - LIS");
// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField username = new TextField();
username.setPromptText("Username");
PasswordField password = new PasswordField();
password.setPromptText("Password");
grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
username.textProperty().addListener((observable, oldValue, newValue) -> {
loginButton.setDisable(newValue.trim().isEmpty());
});
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> username.requestFocus());
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(username.getText(), password.getText());
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
result.ifPresent(usernamePassword -> {
out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
int tryCount = 0;
boolean check_login = true;
do{
if(login(usernamePassword.getKey(),usernamePassword.getValue())){
check_login=false;
tryCount=3;
}else {
++tryCount;
username.clear();
password.clear();
result= dialog.showAndWait();
}
}while(check_login== true && tryCount < 3);
//if(check_login) closeProgram();
});//***************************End of login form**********************
Now i got it to show 3 times by putting "result= dialog.ShowAndWait()" in the do-while loop, but it only captures the data a user entered the first time and not on the last 2 attempts. Sample output:
m1223 //password captured on 1st attempt
m1223//password captured on 2nd attempt but input was m222
m1223//password captured on 3rd attempt but input was m444
How can i make it recapture at every trial...? Thanks in advance.
I tried with standard JavaFX dialogs, and this works:
TextInputDialog dialog = new TextInputDialog("password");
dialog.setContentText("Please enter your password:");
String pwd = null;
int numAttempted = 0;
int MAX_ATTEMPTS = 3;
Optional<String> result;
boolean isCorrectPwd = false;
do {
result = dialog.showAndWait();
numAttempted++;
if (result.isPresent()) {
String res = result.get();
isCorrectPwd = login(res);
if (isCorrectPwd) {
pwd = res;
System.out.println("CORRECT PASSWORD: " + res);
} else {// try again -> loop
System.out.println("WRONG PASSWORD: " + res);
}
}
} while (numAttempted < MAX_ATTEMPTS && !isCorrectPwd);
Problem with your code was that result.ifPresent
was out of the do-while
loop... So the usernamePassword
was assigned only once.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.