Java Code Examples for javafx.scene.control.Dialog#initOwner()

The following examples show how to use javafx.scene.control.Dialog#initOwner() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DialogUtils.java    From mapper-generator-javafx with Apache License 2.0 7 votes vote down vote up
public static void closeDialog(Stage primaryStage) {
    Dialog<ButtonType> dialog = new Dialog<>();

    dialog.setTitle("关闭");

    dialog.setContentText("确认关闭吗?");

    dialog.initOwner(primaryStage);

    dialog.getDialogPane().getButtonTypes().add(ButtonType.APPLY);
    dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);

    dialog.getDialogPane().setPrefSize(350, 100);

    Optional<ButtonType> s = dialog.showAndWait();
    s.ifPresent(s1 -> {
        if (s1.equals(ButtonType.APPLY)) {
            primaryStage.close();
        } else if (s1.equals(ButtonType.CLOSE)) {
            dialog.close();
        }
    });
}
 
Example 2
Source File: Dialogs.java    From CPUSim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initialized a dialog with the given owner window, and String header and content
 * @param dialog dialog box to initialize
 * @param window owning window or null if there is none
 * @param header dialog header
 * @param content dialog content
 */
public static void initializeDialog(Dialog dialog, Window window, String header, String content){
    if (window != null)
        dialog.initOwner(window);
    dialog.setTitle("CPU Sim");
    dialog.setHeaderText(header);
    dialog.setContentText(content);

    // Allow dialogs to resize for Linux
    // https://bugs.openjdk.java.net/browse/JDK-8087981
    dialog.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);

}
 
Example 3
Source File: DialogHelper.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
/** Position dialog relative to another widget
 *
 *  <p>By default, dialogs seem to pop up in the center of the first monitor.
 *  .. even if the "current" window is on a different monitor.
 *
 *  <p>This helper positions the dialog relative to the center
 *  of a node.
 *
 *  @param dialog Dialog to position
 *  @param node Node relative to which dialog should be positioned
 *  @param x_offset Offset relative to center of the node
 *  @param y_offset Offset relative to center of the node
 */
public static void positionDialog(final Dialog<?> dialog, final Node node, final int x_offset, final int y_offset)
{
    dialog.initOwner(node.getScene().getWindow());
    // Must runLater due to dialog Width/Height are initialized after dialog shows up
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            final Bounds pos = node.localToScreen(node.getBoundsInLocal());
            if (pos == null)
            {
                logger.log(Level.WARNING, "Cannot determine dialog position", new NullPointerException());
                return;
            }
            final double nodeX = pos.getMinX();
            final double nodeY = pos.getMinY();

            double newX = nodeX + pos.getWidth()/2 + x_offset;
            double newY = nodeY + pos.getHeight()/2 + y_offset;

            final double dw = dialog.getWidth();
            final double dh = dialog.getHeight();

            List<Screen> scr = Screen.getScreensForRectangle(nodeX, nodeY, pos.getWidth(), pos.getHeight());
            if (scr == null || scr.size() == 0)
            {
                logger.log(Level.WARNING, "Cannot determine dialog position (node out of screen)");
                return;
            }
            // Take the first available screen
            Rectangle2D sb = scr.get(0).getVisualBounds();

            newX = newX < sb.getMinX() ? sb.getMinX() : newX;
            newX = newX + dw > sb.getMaxX() ? sb.getMaxX() - dw : newX;

            newY = newY < sb.getMinY() ? sb.getMinY() : newY;
            newY = newY + dh > sb.getMaxY() ? sb.getMaxY() - dh : newY;

            dialog.setX(newX);
            dialog.setY(newY);

            // Force to front
            forceToFront(dialog);
        }
    });
}