Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Create the custom dialog.
- Dialog<Pair<String, String>> dialog = new Dialog<>();
- Stage s = (Stage) dialog.getDialogPane().getScene().getWindow();
- s.getIcons().add(new Image(this.getClass().getResource("login.png").toString()));
- dialog.setTitle("Authentification");
- dialog.setHeaderText("\tVeuillez saisir vos identifiants \n pour vous connecter à l'application\n\t\t\t\t↓");
- // Set the icon (must be included in the project).
- dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
- // Set the button types.
- ButtonType loginButtonType = new ButtonType("Connexion", 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));
- txtUser.setPromptText("Nom d'utilisateur");
- txtPass.setPromptText("Mot de passe");
- grid.add(new Label("Nom d'utilisateur:"), 0, 0);
- grid.add(txtUser, 1, 0);
- grid.add(new Label("Mot de passe:"), 0, 1);
- grid.add(txtPass, 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).
- txtUser.textProperty().addListener((observable, oldValue, newValue) -> {
- loginButton.setDisable(newValue.trim().isEmpty());
- });
- dialog.getDialogPane().setContent(grid);
- // Request focus on the username field by default.
- Platform.runLater(() -> txtUser.requestFocus());
- // Convert the result to a username-password-pair when the login button
- // is clicked.
- dialog.setResultConverter(dialogButton -> {
- if (dialogButton == loginButtonType) {
- return new Pair<>(txtUser.getText(), txtPass.getText());
- }
- return null;
- });
- Optional<Pair<String, String>> result = dialog.showAndWait();
- result.ifPresent(usernamePassword -> {
- onConnect();
- System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement