Exemplo n.º 1
0
  @Override
  public void start(final Stage primaryStage) {
    // initialize the stage
    primaryStage.setTitle("Modal Confirm Example");
    final WebView webView = new WebView();
    webView.getEngine().load("http://docs.oracle.com/javafx/");
    primaryStage.setScene(new Scene(webView));
    primaryStage.show();

    // initialize the confirmation dialog
    final Stage util = new Stage(StageStyle.TRANSPARENT);
    util.initModality(Modality.APPLICATION_MODAL);
    util.setScene(
        new Scene(
            StackPaneBuilder.create()
                .children(
                    PaneBuilder.create().styleClass("modal-dialog-glass").build(),
                    HBoxBuilder.create()
                        .styleClass("modal-dialog-content")
                        .children(
                            LabelBuilder.create().text("Will you like this page?").build(),
                            ButtonBuilder.create()
                                .text("Yes")
                                .defaultButton(true)
                                .onAction(
                                    new EventHandler<ActionEvent>() {
                                      @Override
                                      public void handle(ActionEvent actionEvent) {
                                        // take action and close the dialog.
                                        System.out.println(
                                            "Liked: " + webView.getEngine().getTitle());
                                        primaryStage.getScene().getRoot().setEffect(null);
                                        util.close();
                                      }
                                    })
                                .build(),
                            ButtonBuilder.create()
                                .text("No")
                                .cancelButton(true)
                                .onAction(
                                    new EventHandler<ActionEvent>() {
                                      @Override
                                      public void handle(ActionEvent actionEvent) {
                                        // abort action and close the dialog.
                                        System.out.println(
                                            "Disliked: " + webView.getEngine().getTitle());
                                        primaryStage.getScene().getRoot().setEffect(null);
                                        util.close();
                                      }
                                    })
                                .build())
                        .build())
                .build(),
            Color.TRANSPARENT));
    util.getScene()
        .getStylesheets()
        .add(getClass().getResource("/assets/fxml/modal-dialog.css").toExternalForm());

    // show the confirmation dialog each time a new page is loaded.
    webView
        .getEngine()
        .getLoadWorker()
        .stateProperty()
        .addListener(
            new ChangeListener<Worker.State>() {
              @Override
              public void changed(
                  ObservableValue<? extends Worker.State> observableValue,
                  Worker.State state,
                  Worker.State newState) {
                if (newState.equals(Worker.State.SUCCEEDED)) {
                  primaryStage.getScene().getRoot().setEffect(new BoxBlur());
                  util.show();
                  util.toFront();
                }
              }
            });
  }
  /**
   * Here is our whole stage with layout, </Br > ( Grid, TextField and Buttons)
   *
   * @author PatrikWebb
   */
  public void NameDisplay() {
    Stage nameWindow = new Stage();

    // Block events to other windows
    nameWindow.initModality(Modality.APPLICATION_MODAL);
    nameWindow.setTitle("Enter your name");
    nameWindow.setMinWidth(250);

    // GridPane Form
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(8);
    grid.setHgap(10);

    // Name Label
    Label nameLabel = new Label("Player Name:");
    GridPane.setConstraints(nameLabel, 0, 0);

    // Name Input
    nameInput = new TextField();
    nameInput.setPromptText("Name");
    nameInput.setFocusTraversable(false);
    GridPane.setConstraints(nameInput, 1, 0);

    // Betting Label
    Label bettingLabel = new Label("Betting amount: ");
    GridPane.setConstraints(bettingLabel, 0, 1);

    // Bett Input
    playerCashInput = new TextField();
    playerCashInput.setFocusTraversable(false);
    playerCashInput.setPromptText("Betting amount");
    GridPane.setConstraints(playerCashInput, 1, 1);

    // Enter button
    Button enterButton = new Button("Enter");
    enterButton.setFocusTraversable(false);
    // setPercentWidth(50);
    GridPane.setConstraints(enterButton, 1, 2);

    // Cancel button
    Button cancelButton = new Button("Demo Player");
    cancelButton.setFocusTraversable(false);
    GridPane.setConstraints(cancelButton, 2, 2);

    //
    enterButton.setOnAction(
        e -> {
          Platform.runLater(
              () -> {
                // Get the input from the nameInput TextField
                name = nameInput.getText();
                // Get the betting input from the bettsInput TextField
                playerCash = Integer.parseInt(playerCashInput.getText());
                // Add name and playerCash input to a new player
                Bank.getInstance().addPlayerToBank(name, playerCash);
                // Add the player to the table
                Bank.getInstance().addPlayersToTheTable();
                // Close the stage
                nameWindow.close();

                System.out.println("\nPlayer Name: " + name);
                System.out.println("Betting Amount: " + playerCash + "\n");
              });
        });

    cancelButton.setOnAction(
        e -> {
          // TODO
          Platform.runLater(
              () -> {

                /* * * * * * * * * * * * *
                 * If you press cancel you still want a player to join
                 * the table so I add a test player insted of
                 * getting the error, Exception JavaFX Application THREAD ;)
                 *  * * * * * * * * * * * */
                Bank.getInstance().addPlayerToBank("Demo Player", 500);
                // Add the player to the table
                Bank.getInstance().addPlayersToTheTable();
                nameWindow.close();
              });
        });

    // Add everything to grid
    grid.getChildren()
        .addAll(nameLabel, nameInput, bettingLabel, playerCashInput, enterButton, cancelButton);

    // Display window and wait for it to be closed before returning
    Scene scene = new Scene(grid, 450, 150);
    nameWindow.setScene(scene);
    nameWindow.showAndWait();
  }