Example usage for java.awt Window setLocation

List of usage examples for java.awt Window setLocation

Introduction

In this page you can find the example usage for java.awt Window setLocation.

Prototype

@Override
public void setLocation(Point p) 

Source Link

Document

The method changes the geometry-related data.

Usage

From source file:Main.java

/**
 * @param owner//w  ww  .j av a 2s.  c o m
 * @param newConfigDialog
 */
public static void centerRelative(java.awt.Window main, java.awt.Window child) {
    java.awt.Dimension dim = main.getSize();
    Point _loc = center(dim, child);
    // Move the window
    _loc.translate(main.getX(), main.getY());
    child.setLocation(_loc);
}

From source file:Main.java

/**
 * Centers the Frame on the screen//from   w  ww . j  a v a2  s  .c  o  m
 */
public static void center(java.awt.Window frame) {
    // Get the size of the screen
    java.awt.Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    Point _loc = center(dim, frame);
    // Move the window
    frame.setLocation(_loc);
}

From source file:Main.java

/**
 * centers the frame in the center of the screen
 *
 * @param pFrame Frame to center//from   w w  w  . java2s.  co  m
 */
static public void centerWindow(Window pFrame) {
    Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    center.x = center.x - (int) pFrame.getSize().getWidth() / 2;
    center.y = center.y - (int) pFrame.getSize().getHeight() / 2;
    pFrame.setLocation(center);
}

From source file:Main.java

public static void centerWindowOnScreen(Window aWindow) {
    Rectangle bounds = getScreenBounds(aWindow);
    Point p = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    Dimension windowSize = aWindow.getSize();
    p.x -= windowSize.width / 2;/* w w w  .j  a v a2  s  . c  om*/
    p.y -= windowSize.height / 2;
    aWindow.setLocation(p);
}

From source file:Main.java

/**
 * Centers a Window on the screen<p>
 * Sets the window on the top left corner if the window's
 * dimensions are bigger than the screen's.
 *
 * @param window a Window object//from   w  w w.ja  v a  2 s  .  c o m
 */
public static void centerOnScreen(Window window) {
    Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    double w = Math.min(window.getWidth(), screen.width);
    double h = Math.min(window.getHeight(), screen.height);
    int x = (int) (center.x - (w / 2));
    int y = (int) (center.y - (h / 2));

    Point corner = new Point((x >= 0 ? x : 0), (y >= 0 ? y : 0));

    window.setLocation(corner);
}

From source file:net.schweerelos.parrot.CombinedParrotApp.java

@SuppressWarnings("serial")
private JToggleButton setupNavigatorButton(final String name, final String accelerator,
        final NavigatorComponent navigator) {
    final Component component = navigator.asJComponent();
    AbstractAction showNavigatorAction = new AbstractAction(name) {
        @Override/*from   w  w w  .j ava2 s. co  m*/
        public void actionPerformed(ActionEvent e) {
            if (!(e.getSource() instanceof JToggleButton)) {
                return;
            }
            final Window window;
            if (component instanceof Window) {
                window = (Window) component;
            } else {
                window = SwingUtilities.getWindowAncestor(component);
            }
            JToggleButton button = (JToggleButton) e.getSource();
            boolean show = button.isSelected();
            if (show) {
                if (window != CombinedParrotApp.this && preferredFrameLocations.containsKey(window)) {
                    window.setLocation(preferredFrameLocations.get(window));
                }
            }
            if (navigator.tellSelectionWhenShown()) {
                Collection<NodeWrapper> selectedNodes = activeMainView.getSelectedNodes();
                navigator.setSelectedNodes(selectedNodes);
            }
            component.setVisible(show);
            if (show) {
                window.setVisible(true);
            } else if (window != CombinedParrotApp.this) {
                window.setVisible(false);
            }
        }
    };
    showNavigatorAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control " + accelerator));
    final JToggleButton button = new JToggleButton(showNavigatorAction);
    button.setToolTipText("Show " + name.toLowerCase());
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            button.setToolTipText((button.isSelected() ? "Hide " : "Show ") + name.toLowerCase());
        }
    });
    final Window window;
    if (component instanceof Window) {
        window = (Window) component;
    } else {
        window = SwingUtilities.getWindowAncestor(component);
    }
    if (window != null) {
        window.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentHidden(ComponentEvent e) {
                button.setSelected(false);
                if (window != CombinedParrotApp.this) {
                    preferredFrameLocations.put(window, window.getLocation());
                }
            }

            @Override
            public void componentShown(ComponentEvent e) {
                button.setSelected(true);
            }
        });
    }
    return button;
}

From source file:org.revager.tools.GUITools.java

/**
 * Sets the location of the given window to cursor position.
 * //from ww w . ja  va 2  s .  c  o m
 * @param win
 *            the window for which the location is to be set
 */
public static void setLocationToCursorPos(Window win) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    double cursorPosX = MouseInfo.getPointerInfo().getLocation().getX();
    double cursorPosY = MouseInfo.getPointerInfo().getLocation().getY();

    double screenWidth = screenSize.getWidth();
    double screenHeight = screenSize.getHeight() - 40;

    double winWidth = win.getSize().getWidth();
    double winHeight = win.getSize().getHeight();

    int winPosX = (int) cursorPosX;
    int winPosY = (int) cursorPosY;
    // If the window would break the screen size
    if (cursorPosX + winWidth > screenWidth) {
        winPosX = (int) (screenWidth - winWidth);
    }
    if (cursorPosY + winHeight > screenHeight) {
        winPosY = (int) (screenHeight - winHeight);
    }
    win.setLocation(new Point(winPosX, winPosY));
}

From source file:util.ui.UiUtilities.java

/**
 * Centers a window to its parent frame and shows it.
 * <p>/*from w  w  w  .  j a va  2s  . co  m*/
 * If the window has no parent frame it will be centered to the screen.
 *
 * @param win
 *          The window to center and show.
 */
public static void centerAndShow(Window win) {
    Dimension wD = win.getSize();
    Dimension frameD;
    Point framePos;
    Frame frame = JOptionPane.getFrameForComponent(win);

    // Should this window be centered to its parent frame?
    boolean centerToParentFrame = (frame != null) && (frame != win) && frame.isShowing();

    // Center to parent frame or to screen
    if (centerToParentFrame) {
        frameD = frame.getSize();
        framePos = frame.getLocation();
    } else {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        // dual head, use first screen
        if (ge.getScreenDevices().length > 1) {
            try {
                GraphicsDevice gd = ge.getDefaultScreenDevice();
                GraphicsConfiguration config = gd.getConfigurations()[0];
                frameD = config.getBounds().getSize();
                framePos = config.getBounds().getLocation();
            } catch (RuntimeException e) {
                frameD = Toolkit.getDefaultToolkit().getScreenSize();
                framePos = new Point(0, 0);
            }
        }
        // single screen only
        else {
            frameD = Toolkit.getDefaultToolkit().getScreenSize();
            framePos = new Point(0, 0);
        }
    }

    Point wPos = new Point(framePos.x + (frameD.width - wD.width) / 2,
            framePos.y + (frameD.height - wD.height) / 2);
    wPos.x = Math.max(0, wPos.x); // Make x > 0
    wPos.y = Math.max(0, wPos.y); // Make y > 0
    win.setLocation(wPos);
    win.setVisible(true);
}