Build an javafx.scene.control.Alert Alert object. - Java JavaFX

Java examples for JavaFX:Alert

Description

Build an javafx.scene.control.Alert Alert object.

Demo Code


/*//from ww  w .ja v a  2s  . co  m
     * Copyright 2015 Thierry Wasylczenko
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *   http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;

public class Main {
  private static final Logger LOGGER = Logger.getLogger(DialogHelper.class.getName());

  /**
   * Build an {@link javafx.scene.control.Alert Alert} object. This method ensures
   * the alert is created in a JavaFX application thread. If the alert can not be
   * created then {@code null} is returned.
   * 
   * @param type
   *          The type of alert to create.
   * @param title
   *          The title of the alert.
   * @param text
   *          The text of this alert.
   * @param buttons
   *          The buttons the alert will contain.
   * @return A well created Alert or {@code null} if an error occurred during the
   *         creation of the alert.
   */
  private static Alert buildAlert(final Alert.AlertType type, final String title, final String text,
      final ButtonType... buttons) {
    final FutureTask<Alert> future = new FutureTask<>(() -> {
      final Alert alert = new Alert(type, text, buttons);
      alert.setGraphic(null);
      alert.setHeaderText(null);
      alert.setTitle(title);
      alert.getDialogPane().getStylesheets().add("/com/twasyl/slideshowfx/css/Default.css");
      return alert;
    });

    PlatformHelper.run(future);

    Alert alert = null;
    try {
      alert = future.get();
    } catch (InterruptedException | ExecutionException e) {
      LOGGER.log(Level.SEVERE, "Can not build an alert", e);
    }

    return alert;
  }
}

Related Tutorials