Position JavaFX dialog relative to another widget By default, dialogs seem to pop up in the center of the first monitor. - Java JavaFX

Java examples for JavaFX:Dialog

Description

Position JavaFX dialog relative to another widget By default, dialogs seem to pop up in the center of the first monitor.

Demo Code

/*******************************************************************************
 * Copyright (c) 2015-2016 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.control.Dialog;

public class Main{
    /** Position dialog relative to another widget
     *//from w ww .j a  v  a 2  s .  c  o  m
     *  <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) {
        final Bounds pos = node.localToScreen(node.getBoundsInLocal());
        dialog.setX(pos.getMinX() + pos.getWidth() / 2 + x_offset);
        dialog.setY(pos.getMinY() + pos.getHeight() / 2 + y_offset);

    }
}

Related Tutorials