Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package customedialogedit;
- import java.net.URL;
- import java.util.Optional;
- import java.util.ResourceBundle;
- import javafx.application.Platform;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.Node;
- import javafx.scene.control.ButtonBar;
- import javafx.scene.control.ButtonType;
- import javafx.scene.control.Dialog;
- import javafx.scene.control.Label;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.util.Pair;
- /**
- *
- * @author syamil
- */
- public class FXMLDocumentController implements Initializable {
- @FXML
- private Label label;
- @FXML
- private void handleButtonAction(ActionEvent event) {
- Dialog<Pair<String, String>> dialog = new Dialog<>();
- dialog.setTitle("Login Dialog");
- dialog.setHeaderText("Look, a Custom Login Dialog");
- // dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
- ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
- dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
- GridPane grid = new GridPane();
- grid.setHgap(10);
- grid.setVgap(10);
- TextField username = new TextField();
- username.setPromptText("Username");
- PasswordField password = new PasswordField();
- password.setPromptText("Password");
- TextField email = new PasswordField();
- email.setPromptText("Email");
- grid.add(new Label("Username:"), 0, 0);
- grid.add(username, 1, 0);
- grid.add(new Label("Password:"), 0, 1);
- grid.add(password, 1, 1);
- grid.add(new Label("Email:"), 0, 2);
- grid.add(email, 1, 2);
- Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
- loginButton.setDisable(true);
- username.textProperty().addListener((observable, oldValue, newValue) -> {
- loginButton.setDisable(newValue.trim().isEmpty());
- });
- dialog.getDialogPane().setContent(grid);
- Platform.runLater(() -> username.requestFocus());
- 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 -> {
- System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
- });
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- // TODO
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement