Advertisement
Guest User

Untitled

a guest
Jun 28th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package customedialogedit;
  7.  
  8. import java.net.URL;
  9. import java.util.Optional;
  10. import java.util.ResourceBundle;
  11. import javafx.application.Platform;
  12. import javafx.event.ActionEvent;
  13. import javafx.fxml.FXML;
  14. import javafx.fxml.Initializable;
  15. import javafx.scene.Node;
  16. import javafx.scene.control.ButtonBar;
  17. import javafx.scene.control.ButtonType;
  18. import javafx.scene.control.Dialog;
  19. import javafx.scene.control.Label;
  20. import javafx.scene.control.PasswordField;
  21. import javafx.scene.control.TextField;
  22. import javafx.scene.layout.GridPane;
  23. import javafx.util.Pair;
  24.  
  25. /**
  26.  *
  27.  * @author syamil
  28.  */
  29. public class FXMLDocumentController implements Initializable {
  30.    
  31.     @FXML
  32.     private Label label;
  33.    
  34.     @FXML
  35.     private void handleButtonAction(ActionEvent event) {
  36.        
  37.         Dialog<Pair<String, String>> dialog = new Dialog<>();
  38.         dialog.setTitle("Login Dialog");
  39.         dialog.setHeaderText("Look, a Custom Login Dialog");
  40.  
  41.  
  42. //        dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
  43.  
  44.  
  45.         ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
  46.         dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
  47.  
  48.  
  49.         GridPane grid = new GridPane();
  50.         grid.setHgap(10);
  51.         grid.setVgap(10);
  52.  
  53.         TextField username = new TextField();
  54.         username.setPromptText("Username");
  55.         PasswordField password = new PasswordField();
  56.         password.setPromptText("Password");
  57.         TextField email = new PasswordField();
  58.         email.setPromptText("Email");
  59.  
  60.         grid.add(new Label("Username:"), 0, 0);
  61.         grid.add(username, 1, 0);
  62.         grid.add(new Label("Password:"), 0, 1);
  63.         grid.add(password, 1, 1);
  64.         grid.add(new Label("Email:"), 0, 2);
  65.         grid.add(email, 1, 2);
  66.  
  67.  
  68.         Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
  69.         loginButton.setDisable(true);
  70.  
  71.  
  72.         username.textProperty().addListener((observable, oldValue, newValue) -> {
  73.             loginButton.setDisable(newValue.trim().isEmpty());
  74.         });
  75.  
  76.         dialog.getDialogPane().setContent(grid);
  77.  
  78.  
  79.         Platform.runLater(() -> username.requestFocus());
  80.  
  81.  
  82.         dialog.setResultConverter(dialogButton -> {
  83.             if (dialogButton == loginButtonType) {
  84.                 return new Pair<>(username.getText(), password.getText());
  85.             }
  86.             return null;
  87.         });
  88.  
  89.         Optional<Pair<String, String>> result = dialog.showAndWait();
  90.  
  91.         result.ifPresent(usernamePassword -> {
  92.             System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
  93.         });
  94.     }
  95.  
  96.    
  97.     @Override
  98.     public void initialize(URL url, ResourceBundle rb) {
  99.         // TODO
  100.     }    
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement