Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. do {
  2.  
  3.  
  4. // Create the custom dialog.
  5. Dialog<Pair<String, String>> dialog = new Dialog<>();
  6. dialog.setTitle("Login Dialog");
  7. dialog.setHeaderText("Look, a Custom Login Dialog");
  8.  
  9.  
  10. //Set the button types.
  11. ButtonType dalejButtonType = new ButtonType("Dalej", ButtonBar.ButtonData.OK_DONE);
  12. ButtonType cancelButton = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
  13. dialog.getDialogPane().getButtonTypes().addAll(dalejButtonType, cancelButton);
  14.  
  15.  
  16. // Create the username and password labels and fields.
  17. GridPane grid = new GridPane();
  18. grid.setHgap(10);
  19. grid.setVgap(10);
  20. grid.setPadding(new Insets(20, 150, 10, 10));
  21.  
  22. TextField imie = new TextField();
  23. imie.setPromptText("Imię");
  24. TextField email = new TextField();
  25. email.setPromptText("eMail");
  26.  
  27. grid.add(new Label("Imię:"), 0, 0);
  28. grid.add(imie, 1, 0);
  29. grid.add(new Label("eMail:"), 0, 1);
  30. grid.add(email, 1, 1);
  31.  
  32. // Enable/Disable login button depending on whether a username was entered.
  33. Node loginButton = dialog.getDialogPane().lookupButton(dalejButtonType);
  34. loginButton.setDisable(true);
  35.  
  36. // Do some validation (using the Java 8 lambda syntax).
  37. imie.textProperty().addListener((observable, oldValue, newValue) -> {
  38. loginButton.setDisable(newValue.trim().isEmpty());
  39. });
  40.  
  41. dialog.getDialogPane().setContent(grid);
  42.  
  43. // Request focus on the username field by default.
  44. Platform.runLater(() -> imie.requestFocus());
  45.  
  46. // Convert the result to a username-password-pair when the login button is clicked.
  47. dialog.setResultConverter(dialogButton -> {
  48. if (dialogButton == dalejButtonType) {
  49. return new Pair<>(imie.getText(), email.getText());
  50. }
  51. if (dialogButton == cancelButton) {
  52. return new Pair<>(imie.getText(), email.getText());
  53. }
  54. return null;
  55.  
  56. });
  57.  
  58.  
  59.  
  60. Optional<Pair<String, String>> result = dialog.showAndWait();
  61.  
  62.  
  63. result.ifPresent(usernamePassword -> {
  64. System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
  65.  
  66. } while(flag);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement