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

Source Link

Document

The method changes the geometry-related data.

Usage

From source file:Main.java

public static Window showCentered(Component parent, Window window) {
    if (null == window)
        throw new IllegalArgumentException("window null");

    if (window instanceof Dialog) {
        Dialog dialog = (Dialog) window;

        boolean isResizable = dialog.isResizable();

        dialog.setResizable(true);//from   www.  j a  va  2 s.  c o m
        dialog.pack();
        dialog.setResizable(isResizable);
    } else {
        window.pack();
    }

    Dimension windowSize = window.getPreferredSize();

    int newX;
    int newY;

    if (null == parent) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        newX = (screen.width - windowSize.width) / 2;
        newY = (screen.height - windowSize.height) / 2;
    } else {
        Dimension parentSize = parent.getSize();
        Point loc = parent.getLocation();

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
        newY = (parentSize.height - windowSize.height) / 2 + loc.y;

        if (0 > newX)
            newX = 0;
        if (0 > newY)
            newY = 0;
    }

    window.setLocation(newX, newY);
    window.setVisible(true);

    return window;
}

From source file:Main.java

static public void centerOnParent(final Window child, final boolean absolute) {
    child.pack();//from   www . j  a va 2 s . c  o  m
    boolean useChildsOwner = child.getOwner() != null
            ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog))
            : false;
    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize;
    final Point parentLocationOnScreen = useChildsOwner ? child.getOwner().getLocationOnScreen()
            : new Point(0, 0);
    final Dimension childSize = child.getSize();
    childSize.width = Math.min(childSize.width, screenSize.width);
    childSize.height = Math.min(childSize.height, screenSize.height);
    child.setSize(childSize);
    int x;
    int y;
    if ((child.getOwner() != null) && child.getOwner().isShowing()) {
        x = (parentSize.width - childSize.width) / 2;
        y = (parentSize.height - childSize.height) / 2;
        x += parentLocationOnScreen.x;
        y += parentLocationOnScreen.y;
    } else {
        x = (screenSize.width - childSize.width) / 2;
        y = (screenSize.height - childSize.height) / 2;
    }
    if (!absolute) {
        x /= 2;
        y /= 2;
    }
    child.setLocation(x, y);
}

From source file:Main.java

public static Window showBelow(Component parent, Window window, int horizontalAlignment) {
    final int DISTANCE = 2;

    if (null == parent)
        throw new IllegalArgumentException("parent null");
    if (null == window)
        throw new IllegalArgumentException("parent null");
    if (!((SwingConstants.LEADING == horizontalAlignment) || (SwingConstants.TRAILING == horizontalAlignment)
            || (SwingConstants.CENTER == horizontalAlignment)
            || (SwingConstants.SOUTH == horizontalAlignment))) {

        throw new IllegalArgumentException("Illegal horizontal alignment " + horizontalAlignment
                + " should be either SwingConstants.LEADING or "
                + "SwingConstants.TRAILING or SwingConstants.CENTER");
    }/*w  w  w.  ja  v a 2  s.co m*/

    window.pack();

    Dimension windowSize = window.getPreferredSize();
    Dimension parentSize = parent.getSize();

    Point loc = parent.getLocationOnScreen();

    int newX;

    if ((SwingConstants.CENTER == horizontalAlignment) || (SwingConstants.SOUTH == horizontalAlignment)) {

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
    } else if (SwingConstants.TRAILING == horizontalAlignment) {
        newX = loc.x + parentSize.width - windowSize.width;
    } else {
        newX = loc.x;
    }

    window.setLocation(newX, (loc.y + parentSize.height + DISTANCE));

    window.setVisible(true);

    return window;
}

From source file:Unicode.java

public void centre(Window c) {
    Dimension us = c.getSize(), them = Toolkit.getDefaultToolkit().getScreenSize();
    int newX = (them.width - us.width) / 2;
    int newY = (them.height - us.height) / 2;
    c.setLocation(newX, newY);
}

From source file:au.org.ala.delta.util.Utils.java

public static void centreWindow(Window c, JFrame frame) {
    Dimension app = frame.getSize();
    int x = frame.getX() + (app.width - c.getWidth()) / 2;
    int y = frame.getY() + (app.height - c.getHeight()) / 3;
    if (y < frame.getY()) {
        y = frame.getY();//from   w  w  w . j a  va  2  s  .  co m
    }
    c.setLocation(x, y);
}

From source file:de.erdesignerng.util.ApplicationPreferences.java

public void setWindowLocation(String aAlias, Window aWindow) {
    try {/*from  w  w  w .  j a va  2s  .co m*/
        int x = Integer.parseInt(windowDefinitions.get(WINDOWXPREFIX + aAlias));
        int y = Integer.parseInt(windowDefinitions.get(WINDOWYPREFIX + aAlias));

        // Only set the size and location if its within the available
        // screen resolution
        Dimension theCurrentScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
        if (x < theCurrentScreenSize.width && y < theCurrentScreenSize.height) {
            aWindow.setLocation(x, y);
        }
    } catch (HeadlessException | NumberFormatException e) {
        // If no old location is known, an Exception is thrown
        // This can be ignored
    }
}

From source file:corelyzer.ui.CorelyzerApp.java

/**
 * Called by the DisplayConfiguration dialog class to begin the creation of
 * the OpenGL windows given previously set parameters of rows and columns of
 * monitors, and the properties of each monitor
 *///  w  ww  .ja  v  a2 s. c  o m
public void createGLWindows() {
    int nrows = preferences.numberOfRows;
    int ncols = preferences.numberOfColumns;
    int tileWidth = preferences.screenWidth;
    int tileHeight = preferences.screenHeight;

    float borderLeft = preferences.borderLeft;
    float borderRight = preferences.borderRight;
    float borderDown = preferences.borderDown;
    float borderUp = preferences.borderUp;

    float screenDpiX = preferences.dpix;
    float screenDpiY = preferences.dpiy;

    int row_offset, column_offset;

    try {
        row_offset = Integer.parseInt(preferences.getProperty("display.row_offset"));
        column_offset = Integer.parseInt(preferences.getProperty("display.column_offset"));
    } catch (NumberFormatException e) {
        row_offset = 0;
        column_offset = 0;
    }

    // brg 1/17/2012: In Windows Vista and 7, Z-order issues with tool windows and the main canvas
    // are abundant and beyond my abilities to fix. We discovered a workaround - reduce the
    // canvas size by a single row/column of pixels, and everything will work properly. Enforce
    // this programatically until we find a fix.
    final String osName = System.getProperty("os.name").toLowerCase();
    final boolean isWindowsCompositingOS = (osName.equals("windows 7") || osName.equals("windows vista"));
    if (isWindowsCompositingOS)
        tileHeight--; // remove one row

    SceneGraph.setCanvasRowcAndColumn(nrows, ncols);

    sharedContext = null;
    int r, c, canvasNum = 0;
    for (r = 0; r < nrows; r++) {
        for (c = 0; c < ncols; c++) {
            // Allow alpha GL context
            GLProfile profile = GLProfile.getDefault();
            GLCapabilities cap = new GLCapabilities(profile);//GLProfile.getDefault() );
            cap.setAlphaBits(8);
            // System.out.println("---> GL " + cap.toString());

            /*
             * if(MAC_OS_X) { win = new JFrame(); ((JFrame)
             * win).setUndecorated(true); } else { win = new JWindow(); }
             */

            Window win = new JFrame();
            ((JFrame) win).setUndecorated(true);

            win.setLocation(c * tileWidth + column_offset, r * tileHeight + row_offset);

            // brg 3/16/2012: Once we have a shared context, it must be passed in the constructor.
            // The setContext() method doesn't work. (JOGL bug?)
            GLCanvas cvs = null;
            if (sharedContext != null)
                cvs = new GLCanvas(cap, null, sharedContext, null);
            else
                cvs = new GLCanvas(cap);

            win.add(cvs);
            win.addWindowFocusListener(new WindowFocusListener() {
                public void windowGainedFocus(final WindowEvent event) {
                    // do nothing
                }

                public void windowLostFocus(final WindowEvent event) {
                    String isCanvasAlwaysBelow = preferences.getProperty("ui.canvas.alwaysBelow");

                    boolean b;
                    try {
                        b = Boolean.parseBoolean(isCanvasAlwaysBelow);
                    } catch (Exception e) {
                        b = true;
                    }

                    if (b) {
                        GLWindowsToBack();
                    }
                }
            });

            canvasNum++;

            windowVec.add(win);

            final float px = tileWidth * c + (borderLeft + borderRight) * screenDpiX * c;
            final float py = tileHeight * r + (borderUp + borderDown) * screenDpiY * r;
            final int id = SceneGraph.genCanvas(px, py, tileWidth, tileHeight, screenDpiX, screenDpiY);

            CorelyzerGLCanvas cglc = new CorelyzerGLCanvas(cvs, tileWidth, tileHeight, px, py, id);
            canvasVec.add(cglc);

            // if it's the bottom most screen or the first column,
            // then mark to draw depth scale
            if (c == 0) {
                SceneGraph.setCanvasFirstColumn(cglc.getCanvasID(), true);
            }

            if (r == nrows - 1) {
                SceneGraph.setCanvasBottomRow(cglc.getCanvasID(), true);
            }

            win.pack();
            win.setVisible(true);

            // brg 3/16/2012: In JOGL2, a GLCanvas's context is only usable after the
            // canvas has been made visible. Grab the context from the first canvas
            // and share with subsequent canvases at construction-time.
            if (sharedContext == null)
                sharedContext = cvs.getContext();

            win.toBack();
        }
    }

    createTrackMenuItem.setEnabled(true);
    loadDataMenuItem.setEnabled(true);
    loadStateFileMenuItem.setEnabled(true);

    isGLInited = true;
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Center and make the window visible// w w  w. ja  v a 2 s .  co  m
 * @param window the window to center
 */
public static void positionAndShow(java.awt.Window window) {
    if (System.getProperty("os.name").equals("Mac OS X")) {
        window.setLocation(0, 0);
        window.setVisible(true);

    } else {
        centerAndShow(window);
    }

}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Center and make the window visible/*from   w w w. j a  va2 s .  c  om*/
 * @param window the window to center
 * @param width sets the dialog to this width (can be null)
 * @param height sets the dialog to this height (can be null)
 */
public static void centerWindow(final java.awt.Window window, final Integer width, final Integer height) {
    JFrame topFrame = (JFrame) UIRegistry.getTopWindow();
    Insets screenInsets = null;
    Rectangle screenRect = null;

    if (width != null || height != null) {
        Dimension s = window.getSize();
        if (width != null)
            s.width = width;
        if (height != null)
            s.height = height;
        window.setSize(s);
    }

    // if there is a registered TOPFRAME, and it's not the same as the window being passed in...
    if (topFrame != null && topFrame != window) {
        screenRect = topFrame.getBounds();
        screenInsets = topFrame.getInsets();
    } else {
        screenRect = window.getGraphicsConfiguration().getBounds();
        screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(window.getGraphicsConfiguration());
    }

    // Make sure we don't place the demo off the screen.
    int centerWidth = screenRect.width < window.getSize().width ? screenRect.x
            : screenRect.x + screenRect.width / 2 - window.getSize().width / 2;
    int centerHeight = screenRect.height < window.getSize().height ? screenRect.y
            : screenRect.y + screenRect.height / 2 - window.getSize().height / 2;

    centerHeight = centerHeight < screenInsets.top ? screenInsets.top : centerHeight;

    window.setLocation(centerWidth, centerHeight);
}

From source file:com.jcraft.weirdx.XWindow.java

XWindow(int wid, XWindow prnt, int x, int y, int width, int height, int bwidth, int clss, byte depth,
        Client client, int visual, int msk) throws IOException {
    this(wid);/*from  www  .j a v  a  2s. c o  m*/
    WindowOpt opt;
    this.client = client;
    this.parent = prnt;
    screen = prnt.screen;

    if (clss == CopyFromParent)
        clss = prnt.clss;
    this.clss = clss;

    if ((clss != InputOutput) && (clss != InputOnly)) {
        client.errorValue = clss;
        client.errorReason = 2; // BadValue;
        return;
    }

    if ((clss != InputOnly) && (prnt.clss == InputOnly)) {
        client.errorValue = clss;
        client.errorReason = 8; // BadMatch;
        return;
    }
    if ((clss == InputOnly) && ((bwidth != 0) || (depth != 0))) {
        client.errorValue = 0;
        client.errorReason = 8; // BadMatch;
        return;
    }

    if ((clss == InputOutput) && (depth == 0)) {
        depth = prnt.depth;
    }

    opt = prnt.optional;
    if (opt == null) {
        opt = prnt.findOptional().optional;
    }
    if (visual == CopyFromParent) {
        visual = opt.visual;
    }
    if ((visual != opt.visual) || (depth != prnt.depth)) {
        boolean foo = false;
        Depth pdepth;
        for (int i = 0; i < screen.depth.length; i++) {
            pdepth = screen.depth[i];
            if (depth == pdepth.depth || depth == 0) {
                if (pdepth.visual != null) {
                    for (int j = 0; j < pdepth.visual.length; j++) {
                        if (visual == pdepth.visual[j].id) {
                            foo = true;
                            break;
                        }
                    }
                }
            }
        }
        if (!foo) {
            client.errorValue = 0;
            client.errorReason = 8; // BadMatch;
            return;
        }
    }
    if (((msk & (CWBorderPixmap | CWBorderPixel)) == 0) && (clss != InputOnly) && (depth != prnt.depth)) {
        client.errorValue = 0;
        client.errorReason = 8; // BadMatch;
        return;
    }

    this.depth = depth;
    if (depth == prnt.depth) {
        this.bitsPerPixel = prnt.bitsPerPixel;
    } else {
        int ii = 0;
        while (ii < Format.format.length) {
            if (Format.format[ii].depth == screen.rootDepth)
                break;
            ii++;
        }
        if (ii == Format.format.length) {
            // ???
            ii = 0;
        }
        this.bitsPerPixel = Format.format[ii].bpp;
    }

    this.type = prnt.type;
    if (clss == InputOnly)
        this.type = UNDRAWABLE_WINDOW;

    setDefault();

    if (visual != opt.visual) {
        makeOptional();
        this.optional.visual = visual;
        this.optional.colormap = screen.defaultColormap;
    }
    this.borderWidth = bwidth;
    attr &= ~backgroundState;
    attr &= ~borderIsPixel;
    attr |= (prnt.attr & borderIsPixel);

    this.border = prnt.border.dup();

    if ((attr & borderIsPixel) == 0) {
        this.border.pixmap.ref();
    }
    this.origin.x = (short) (x + bwidth);
    this.origin.y = (short) (y + bwidth);
    this.width = width;
    this.height = height;
    this.x = (short) (prnt.x + x + bwidth);
    this.y = (short) (prnt.y + y + bwidth);

    synchronized (LOCK) {
        this.nextSib = prnt.firstChild;
        if (prnt.firstChild != null)
            prnt.firstChild.prevSib = this;
        else
            prnt.lastChild = this;
        prnt.firstChild = this;
    }

    if ((msk & CWEventMask) == 0) {
        recalculateDeliverableEvents();
    }

    msk &= 0x7fff;
    if (msk != 0) {
        changeAttr(client, msk);
    }

    if (client.errorReason != 0) {
        this.delete();
        return;
    }

    if ((msk & CWBackingStore) != 0 && (defaultBackingStore != 0)) {
        attr &= ~backingStore;
        attr |= (defaultBackingStore << backingStoreOffset);
        attr |= forcedBS;
    }

    //  ddxwindow=new DDXWindow();
    try {
        ddxwindow = (DDXWindow) dDXWindow.newInstance();
    } catch (Exception e) {
        LOG.error(e);
        /*ddxwindow=new DDXWindow();*/ }
    ddxwindow.init(this);

    try {
        if (screen.windowmode != WeirdX.InBrowser && prnt == screen.root) {
            final java.awt.Window frame = getFrame();
            /*
            if(frame instanceof JFrame){                               
              ((JFrame)frame).setJMenuBar(null);                       
              ((JFrame)frame).getContentPane().setLayout(null);        
                ((JFrame)frame).setResizable(false);                                 
            }                                                          
            else*/ if (frame instanceof Frame) {
                ((Frame) frame).setMenuBar(null);
                ((Frame) frame).setResizable(true);
            }

            ddxwindow.setLocation(0, 0);

            //   if(frame instanceof JFrame){                               
            //     ((JFrame)frame).getContentPane().add((java.awt.Component)ddxwindow);
            //   }                                                          
            //   else{                                                      
            frame.add((java.awt.Component) ddxwindow);
            //   }                                                          

            frame.pack();
            Insets insets = frame.getInsets();
            /*
            frame.setSize(this.width+this.borderWidth*2+             
                     insets.left+insets.right,                  
                     this.height+this.borderWidth*2+            
                     insets.bottom+insets.top);                 
            */
            frame.setLocation(this.origin.x - this.borderWidth + parent.borderWidth,
                    this.origin.y - this.borderWidth + parent.borderWidth);

            if (frame instanceof Frame) {
                addWindowListener((java.awt.Frame) frame);
                addComponentListener((java.awt.Frame) frame);
            }
        } else {
            ddxwindow.setLocation(origin.x - borderWidth + parent.borderWidth,
                    origin.y - borderWidth + parent.borderWidth);
            prnt.ddxwindow.add((java.awt.Component) ddxwindow, 0);
        }
        if ((attr & cursorIsNone) == 0) {
            XCursor cur = getCursor();
            if (cur != null) {
                ddxwindow.setCursor(cur.cursor);
            }
        }
        if (bwidth > 0) {
            ddxwindow.setBorderPixmap(border.pixmap);
        }
    } catch (Exception ee) {
        //System.out.println("error: Window?? "+ee);
    }

    if (prnt.subSend()) {
        client.cevent.mkCreateNotify(prnt.id, id, x, y, width, height, borderWidth,
                (attr & overrideRedirect) != 0 ? 0 : 1);
        prnt.sendEvent(client.cevent, 1, null);
    }
}