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(int x, int y) 

Source Link

Document

Moves this component to a new location.

Usage

From source file:GUIUtils.java

/**
 * Centers <CODE>wind</CODE> within the passed rectangle.
 * //  w  ww  .  jav a2  s.  co  m
 * @param wind
 *          The Window to be centered.
 * @param rect
 *          The rectangle (in screen coords) to center <CODE>wind</CODE>
 *          within.
 * 
 * @throws IllegalArgumentException
 *           If <TT>Window</TT> or <TT>Rectangle</TT> is <TT>null</TT>.
 */
private static void center(Component wind, Rectangle rect) {
    if (wind == null || rect == null) {
        throw new IllegalArgumentException("null Window or Rectangle passed");
    }
    Dimension windSize = wind.getSize();
    int x = ((rect.width - windSize.width) / 2) + rect.x;
    int y = ((rect.height - windSize.height) / 2) + rect.y;
    if (y < rect.y) {
        y = rect.y;
    }
    wind.setLocation(x, y);
}

From source file:Main.java

/**
 * Centra una finestra all'interno di un'altra
 *
 * @param f componente target (finestra esterna) - se null centra nello
 * schermo/*from   w  w  w  .j ava 2  s.  c  om*/
 * @param c componente da centrare (finestra interna)
 */
public static void centerForm(Component f, Component c) {
    Rectangle rf = new Rectangle();
    ;
    if (f == null) {
        rf = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    } else {
        rf = f.getBounds();
    }
    Rectangle rc = c.getBounds();

    int x = (rf.width - rc.width) / 2;
    if (x < 5) {
        x = 5;
    }
    int y = (rf.height - rc.height) / 2;
    if (y < 5) {
        y = 5;
    }

    if (f == null) {
        c.setLocation(x, y);
    } else {
        c.setLocation(x + f.getX(), y + f.getY());
    }
}

From source file:Main.java

public static void 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   w  ww.  j  a v a  2  s  . co  m
        newComponentX = 0;
    int newComponentY = screenSize.height - componentSize.height;
    if (newComponentY >= 0)
        newComponentY = newComponentY / 2;
    else
        newComponentY = 0;
    component.setLocation(newComponentX, newComponentY);
}

From source file:Main.java

/**
 * Centers a component on its parent./*from w  w w  .  j av  a2 s.  co m*/
 *
 * @param component
 */
public static void centerInParent(Component component) {
    Container parent = component.getParent();
    if (parent != null) {
        Rectangle parentBounds = parent.getBounds();
        Rectangle dialogBounds = new Rectangle(
                (int) (parentBounds.getMinX() + parentBounds.getWidth() / 2 - component.getWidth() / 2),
                (int) (parentBounds.getMinY() + parentBounds.getHeight() / 2 - component.getHeight() / 2),
                component.getWidth(), component.getHeight());
        //dialog.setBounds( dialogBounds );
        component.setLocation(dialogBounds.x, dialogBounds.y);
    }
}

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

/**
 * Prepares {@link Component} for printing.
 * /*from   w  w w  .  ja  va 2  s .c o  m*/
 * mitin_aa: Linux: for Metacity window manager (as recommended to use with the Designer) to
 * prevent preview window flickering a better place for preview window is right-bottom screen
 * direction.
 * 
 * TODO: add a preference (Linux only) allowing the user to explicitly set preview window location
 */
public static void prepareForPrinting(Component component) throws Exception {
    component.setLocation(10000, 10000);
    // don't grab focus during printing
    if (component instanceof Window) {
        Window window = (Window) component;
        m_fosucableStates.put(window, window.getFocusableWindowState());
        window.setFocusableWindowState(false);
    }
    // make visible
    setVisible(component, true);
    {
        // workaround to prevent window from flashing if the Window Manager 
        // doesn't allow the window to appear off-screen. 
        if (component instanceof Window) {
            Window window = (Window) component;
            window.toBack();
            // do the location change once again, because sometimes setLocation() 
            // for invisible windows could be ignored.
            component.setLocation(10000, 10000);
        }
    }
}

From source file:org.xulux.swing.util.NativeWidgetHandler.java

/**
 * @see org.xulux.nyx.gui.INativeWidgetHandler#setLocationOnWidget(java.lang.Object, int, int)
 *//* w w  w  . j  av a 2 s .c o  m*/
public void setLocationOnWidget(Object widget, int x, int y) {
    if (!(widget instanceof Component)) {
        return;
    }
    Component comp = (Component) widget;
    comp.setLocation(x, y);
}

From source file:Main.java

/**
 * Centers the given component over another component.
 * <p/>/*from w  ww  . jav a2s  . com*/
 * <p> The method performs the alignment by setting a newly computed location for the component. It does not alter
 * the component's size.
 *
 * @param comp      the component whose location is to be altered
 * @param alignComp the component used for the alignment of the first component, if <code>null</code> the component
 *                  is ceneterd within the screen area
 *
 * @throws IllegalArgumentException if the component is <code>null</code>
 */
public static void centerComponent(Component comp, Component alignComp) {

    if (comp == null) {
        throw new IllegalArgumentException("comp must not be null");
    }

    Dimension compSize = comp.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    int x1, y1;

    if (alignComp != null && !new Rectangle(alignComp.getSize()).isEmpty()) {
        Point alignCompOffs = alignComp.getLocation();
        Dimension alignCompSize = alignComp.getSize();
        x1 = alignCompOffs.x + (alignCompSize.width - compSize.width) / 2;
        y1 = alignCompOffs.y + (alignCompSize.height - compSize.height) / 2;
    } else {
        x1 = (screenSize.width - compSize.width) / 2;
        y1 = (screenSize.height - compSize.height) / 2;
    }

    int x2 = x1 + compSize.width;
    int y2 = y1 + compSize.height;

    if (x2 >= screenSize.width) {
        x1 = screenSize.width - compSize.width - 1;
    }
    if (y2 >= screenSize.height) {
        y1 = screenSize.height - compSize.height - 1;
    }
    if (x1 < 0) {
        x1 = 0;
    }
    if (y1 < 0) {
        y1 = 0;
    }

    comp.setLocation(x1, y1);
}

From source file:io.github.tavernaextras.biocatalogue.model.Util.java

/**
 * Makes sure that one component (for example, a window) is centered horizontally and vertically
 * relatively to the other component. This is achieved by aligning centers of the two components.
 * // w ww  .  ja  va  2 s. c o m
 * This method can be used even if the 'dependentComponent' is larger than the 'mainComponent'. In
 * this case it is probably only useful for centering windows against each other rather than
 * components inside a container.
 * 
 * Method also makes sure that the dependent component will not be placed above the screen's upper
 * edge and to the left of the left edge.
 */
public static void centerComponentWithinAnother(Component mainComponent, Component dependentComponent) {
    int iMainComponentCenterX = (int) Math
            .round(mainComponent.getLocationOnScreen().getX() + (mainComponent.getWidth() / 2));
    int iPosX = iMainComponentCenterX - (dependentComponent.getWidth() / 2);
    if (iPosX < 0)
        iPosX = 0;

    int iMainComponentCenterY = (int) Math
            .round(mainComponent.getLocationOnScreen().getY() + (mainComponent.getHeight() / 2));
    int iPosY = iMainComponentCenterY - (dependentComponent.getHeight() / 2);
    if (iPosY < 0)
        iPosY = 0;

    dependentComponent.setLocation(iPosX, iPosY);
}

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

/**
 * Centers a {@code Component} to the screen.
 *
 * @param dialog JDialog dialog to center
 *//*from   w w  w.jav a 2  s.c o  m*/
public static void centerComponent(Component dialog) {
    // since the Toolkit.getScreenSize() method is broken in the Linux implementation
    // of Java 5  (it returns double the screen size under xinerama), this method is
    // encapsulated to accomodate this with a hack.
    // TODO: remove the hack, once Java fixed this.
    // final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    final Rectangle screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration().getBounds();

    final Dimension dialogSize = dialog.getSize();

    if (dialogSize.height > screenSize.height) {
        dialogSize.height = screenSize.height;
    }

    if (dialogSize.width > screenSize.width) {
        dialogSize.width = screenSize.width;
    }
    dialog.setSize(dialogSize);

    dialog.setLocation(screenSize.x + ((screenSize.width - dialogSize.width) / 2),
            screenSize.y + ((screenSize.height - dialogSize.height) / 2));
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static void centerComponent(Component component) {
    component.setLocation((component.getParent().getWidth() - component.getWidth()) / 2,
            (component.getParent().getHeight() - component.getHeight()) / 2);
}