Advertisement
Guest User

LoginCode

a guest
Feb 21st, 2016
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. // Create the custom dialog.
  2.         Dialog<Pair<String, String>> dialog = new Dialog<>();
  3.         Stage s = (Stage) dialog.getDialogPane().getScene().getWindow();
  4.         s.getIcons().add(new Image(this.getClass().getResource("login.png").toString()));
  5.         dialog.setTitle("Authentification");
  6.         dialog.setHeaderText("\tVeuillez saisir vos identifiants \n  pour vous connecter à l'application\n\t\t\t\t↓");
  7.  
  8.         // Set the icon (must be included in the project).
  9.         dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
  10.  
  11.         // Set the button types.
  12.         ButtonType loginButtonType = new ButtonType("Connexion", ButtonData.OK_DONE);
  13.         dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
  14.  
  15.         // Create the username and password labels and fields.
  16.         GridPane grid = new GridPane();
  17.         grid.setHgap(10);
  18.         grid.setVgap(10);
  19.         grid.setPadding(new Insets(20, 150, 10, 10));
  20.  
  21.        
  22.         txtUser.setPromptText("Nom d'utilisateur");
  23.         txtPass.setPromptText("Mot de passe");
  24.  
  25.         grid.add(new Label("Nom d'utilisateur:"), 0, 0);
  26.         grid.add(txtUser, 1, 0);
  27.         grid.add(new Label("Mot de passe:"), 0, 1);
  28.         grid.add(txtPass, 1, 1);
  29.  
  30.         // Enable/Disable login button depending on whether a username was
  31.         // entered.
  32.         Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
  33.         loginButton.setDisable(true);
  34.  
  35.         // Do some validation (using the Java 8 lambda syntax).
  36.         txtUser.textProperty().addListener((observable, oldValue, newValue) -> {
  37.             loginButton.setDisable(newValue.trim().isEmpty());
  38.         });
  39.  
  40.         dialog.getDialogPane().setContent(grid);
  41.  
  42.         // Request focus on the username field by default.
  43.         Platform.runLater(() -> txtUser.requestFocus());
  44.  
  45.         // Convert the result to a username-password-pair when the login button
  46.         // is clicked.
  47.         dialog.setResultConverter(dialogButton -> {
  48.             if (dialogButton == loginButtonType) {
  49.                 return new Pair<>(txtUser.getText(), txtPass.getText());
  50.             }
  51.             return null;
  52.         });
  53.  
  54.         Optional<Pair<String, String>> result = dialog.showAndWait();
  55.  
  56.         result.ifPresent(usernamePassword -> {
  57.             onConnect();
  58.             System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
  59.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement