Example usage for java.awt Component setLocation

List of usage examples for java.awt Component setLocation

Introduction

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

Prototype

public void setLocation(Point p) 

Source Link

Document

Moves this component to a new location.

Usage

From source file:Main.java

public static void center(Component parent, Component comp) {
    comp.setLocation(getCenterPoint(parent, comp));
}

From source file:Main.java

static public void fitComponentIntoScreen(Component component, Point location) {
    Point newLocation = fitComponentInsideScreen(component, location);

    component.setLocation(newLocation);
}

From source file:Main.java

static public void alignComponents(Component child, Component parent) {
    Point location = parent.getLocation();
    Dimension parentSize = parent.getSize();
    Dimension childSize = child.getSize();

    if ((location.x - childSize.width) > 0) {
        location.x -= childSize.width;// w ww . j  av  a  2s. c o m
        child.setLocation(location);
    } else if ((location.x + parentSize.width + childSize.width) < SCREEN_SIZE.width) {
        location.x += parentSize.width;
        child.setLocation(location);
    } else
        centerComponent(child, location, parentSize);
}

From source file:Main.java

public static void center(Component c) {
    Dimension screenSize = c.getToolkit().getScreenSize();
    Dimension componentSize = c.getSize();
    int xPos = (screenSize.width - componentSize.width) / 2;
    xPos = Math.max(xPos, 0);/*from ww w .  j  a v a 2s  . c om*/
    int yPos = (screenSize.height - componentSize.height) / 2;
    yPos = Math.max(yPos, 0);
    c.setLocation(new Point(xPos, yPos));
}

From source file:Main.java

public static void center(Component c) {
    Dimension screenSize = c.getToolkit().getScreenSize();
    screenSize.width -= BORDER_SIZE;//from   ww  w . ja va  2  s  .  c om
    screenSize.height -= BORDER_SIZE;
    Dimension componentSize = c.getSize();
    int xPos = (screenSize.width - componentSize.width) / 2;
    xPos = Math.max(xPos, 0);
    int yPos = (screenSize.height - componentSize.height) / 2;
    yPos = Math.max(yPos, 0);
    c.setLocation(new Point(xPos, yPos));
}

From source file:Main.java

public static Point moveToScreenCenter(Component component) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension componentSize = component.getSize();
    int newComponentX = screenSize.width - componentSize.width;
    if (newComponentX >= 0)
        newComponentX = newComponentX / 2;
    else/*from   ww w.  j  a  v  a 2s .co  m*/
        newComponentX = 0;
    int newComponentY = screenSize.height - componentSize.height;
    if (newComponentY >= 0)
        newComponentY = newComponentY / 2;
    else
        newComponentY = 0;
    Point location = new Point(newComponentX, newComponentY);
    component.setLocation(location);
    return location;
}

From source file:Main.java

public static void centralizeComponent(Component component, Component otherComponent) {

    Dimension othersDimension = null;
    Point othersLocation = null;/*  w w  w.j  av  a  2 s  .com*/
    if (otherComponent == null || !otherComponent.isVisible()) {
        othersDimension = Toolkit.getDefaultToolkit().getScreenSize();
        othersLocation = new Point(0, 0);
    } else {
        othersDimension = otherComponent.getSize();
        othersLocation = otherComponent.getLocation();
    }
    Point centerPoint = new Point(
            (int) Math.round(othersDimension.width / HALF - component.getWidth() / HALF) + othersLocation.x,
            (int) Math.round(othersDimension.height / HALF - component.getHeight() / HALF) + othersLocation.y);
    component.setLocation(centerPoint);
}

From source file:Main.java

public static void setCenter(Component component, Component component1) {
    Point point;//from  w w  w . j av  a  2s  .  co  m
    Dimension dimension;
    if (component != null) {
        point = component.getLocation();
        dimension = component.getSize();
    } else {
        dimension = Toolkit.getDefaultToolkit().getScreenSize();
        point = new Point(0, 0);
    }
    Dimension dimension1 = Toolkit.getDefaultToolkit().getScreenSize();
    point.setLocation((double) point.x + dimension.getWidth() / 2D,
            (double) point.y + dimension.getHeight() / 2D);
    Point point1 = new Point(point.x - component1.getWidth() / 2, point.y - component1.getHeight() / 2);
    if (point1.y < 0)
        point1.y = 0;
    if (point1.x < 0)
        point1.y = 0;
    if (point1.x + component1.getWidth() > dimension1.width)
        point1.x = dimension1.width - component1.getWidth();
    if (point1.y + component1.getHeight() > dimension1.height)
        point1.y = dimension1.height - component1.getHeight();
    component1.setLocation(point1);
}

From source file:org.eclipse.wb.internal.swing.utils.SwingImageUtils.java

private static void fetchMenuVisualData_JMenu_JPopupMenu(MenuVisualData menuData, Container menuObject)
        throws Exception {
    JPopupMenu popupMenu = menuObject instanceof JPopupMenu ? (JPopupMenu) menuObject
            : ((JMenu) menuObject).getPopupMenu();
    // image//  w  w  w  .  j  a  v a  2s.  c  om
    {
        prepareForPrinting(popupMenu);
        // OSX Java since jdk 1.6.0_20 requires menu invoker to be visible.
        // traverse parents until null or already visible and make sure that all are visible.
        // CHECK: it could flash on Windows.
        Point parentLocation = null;
        Component parent = popupMenu.getInvoker();
        while (parent != null && !parent.isShowing()) {
            Container parent2 = parent.getParent();
            if (parent2 != null) {
                parent = parent2;
            } else {
                break;
            }
        }
        if (parent != null) {
            parentLocation = parent.getLocation();
            prepareForPrinting(parent);
        }
        // fetch image
        try {
            Container popupMenuParent = popupMenu.getParent();
            if (popupMenuParent != null) {
                popupMenuParent.doLayout();
            }
            popupMenu.doLayout();
            menuData.m_menuImage = createComponentShot(popupMenu);
        } finally {
            setVisible(popupMenu, false);
            if (parent != null) {
                parent.setLocation(parentLocation);
                if (parent instanceof JPopupMenu) {
                    setVisible(parent, false);
                }
            }
        }
    }
    // bounds
    {
        org.eclipse.swt.graphics.Rectangle imageBounds = menuData.m_menuImage.getBounds();
        menuData.m_menuBounds = new Rectangle(0, 0, imageBounds.width, imageBounds.height);
    }
    // items
    fetchMenuVisualData_items(menuData, popupMenu);
}

From source file:pcgen.gui2.tools.Utility.java

/**
 * Centres the dialog over the component ensuring that the dialog will be
 * within the usable area of the screen (i.e. excluding native task bars,
 * menus bars etc)./*from   w w w.  ja va 2s .  c om*/
 *
 * @param parent The component over which the dialog should be centred.
 * @param dialog The dialog to be positioned.
 */
public static void setComponentRelativeLocation(Component parent, Component dialog) {
    // First make sure it is not too big
    resizeComponentToScreen(dialog);

    // Get the maximum window size to account for taskbars etc
    Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    Point centreOfParent = new Point(parent.getWidth() / 2, parent.getHeight() / 2);
    SwingUtilities.convertPointToScreen(centreOfParent, parent);
    // Default to centre of parent
    Point location = new Point(centreOfParent.x - (dialog.getWidth() / 2),
            centreOfParent.y - (dialog.getHeight() / 2));
    // Adjust so it fits on the screen
    if ((location.x + dialog.getWidth()) > (screenBounds.width + screenBounds.x)) {
        location.x -= (location.x + dialog.getWidth()) - (screenBounds.width + screenBounds.x);
    }
    if (location.x < screenBounds.x) {
        location.x = screenBounds.x;
    }
    if ((location.y + dialog.getHeight()) > (screenBounds.height + screenBounds.y)) {
        location.y -= (location.y + dialog.getHeight()) - (screenBounds.height + screenBounds.y);
    }
    if (location.y < screenBounds.y) {
        location.y = screenBounds.y;
    }
    dialog.setLocation(location);
}