Example usage for java.awt Component getLocation

List of usage examples for java.awt Component getLocation

Introduction

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

Prototype

public Point getLocation() 

Source Link

Document

Gets the location of this component in the form of a point specifying the component's top-left corner.

Usage

From source file:Main.java

/**
 * Centers the given component over another component.
 * <p/>/*from  w  w w  . j  a v a 2 s  .  c o  m*/
 * <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: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   ww  w .ja  v  a 2s.  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: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/*from w w w  . ja v  a 2  s  .  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:Main.java

public void setView(JScrollPane scroll, Component comp) {
    JViewport view = scroll.getViewport();
    Point p = comp.getLocation();
    view.setViewPosition(p);/*from   w w  w .  jav a  2  s . c  o  m*/
}

From source file:ComponentEventDemo.java

public void componentMoved(ComponentEvent e) {
    Component c = e.getComponent();
    displayMessage("componentMoved event from " + c.getClass().getName() + "; new location: "
            + c.getLocation().x + ", " + c.getLocation().y);
}

From source file:org.n52.oxf.ui.swing.ChartDialog.java

/**
 * This is the default constructor/*w  ww.  j  a  v  a  2s  .  c o  m*/
 */
public ChartDialog(Component owner, ParameterContainer paramCon, OXFFeatureCollection observations,
        IServiceAdapter adapter, ServiceDescriptor descriptor, IFeatureStore featureStore,
        IChartRenderer chartRenderer) {
    setTitle("Chart View");
    setLocation(owner.getLocation());

    this.paramCon = paramCon;
    this.observations = observations;
    this.adapter = adapter;
    this.descriptor = descriptor;
    this.featureStore = featureStore;
    this.chartRenderer = chartRenderer;

    // TODO: this maybe cause a ClassCastException, because time might be of type ITimePosition -->
    TimePeriod timePeriod = (TimePeriod) paramCon.getParameterShellWithCommonName(Parameter.COMMON_NAME_TIME)
            .getSpecifiedValue();

    timePeriodPanel = new TimePeriodPanel(timePeriod);

    applyButton = new JButton("Apply");
    applyButton.addActionListener(this);

    nowButton = new JButton("Now");
    nowButton.addActionListener(this);

    initialize();
    initChartPanel(observations, paramCon);
}

From source file:edu.ku.brc.specify.prefs.SystemPrefs.java

/**
 * //from  ww w.jav a  2s  .  com
 */
protected void clearCache() {
    final String CLEAR_CACHE = "CLEAR_CACHE";
    final JStatusBar statusBar = UIRegistry.getStatusBar();
    statusBar.setIndeterminate(CLEAR_CACHE, true);

    Component dlg = getParent();
    while (dlg != null && !(dlg instanceof JDialog)) {
        dlg = dlg.getParent();
    }
    Point loc = null;
    if (dlg != null) {
        loc = dlg.getLocation();
    }

    final JDialog parentDlg = (JDialog) dlg;

    Rectangle screenRect = dlg.getGraphicsConfiguration().getBounds();
    parentDlg.setLocation(loc.x, screenRect.height);

    javax.swing.SwingWorker<Integer, Integer> backupWorker = new javax.swing.SwingWorker<Integer, Integer>() {
        @Override
        protected Integer doInBackground() throws Exception {
            try {
                long startTm = System.currentTimeMillis();

                Specify.getCacheManager().clearAll();

                // Tell the OK btn a change has occurred and update the OK btn
                FormValidator validator = ((FormViewObj) form).getValidator();
                if (validator != null) {
                    validator.setHasChanged(true);
                    validator.wasValidated(null);
                    validator.dataChanged(null, null, null);
                }

                Thread.sleep(Math.max(0, 2000 - (System.currentTimeMillis() - startTm)));

            } catch (Exception ex) {
            }

            return null;
        }

        @Override
        protected void done() {
            super.done();

            statusBar.setProgressDone(CLEAR_CACHE);

            UIRegistry.clearSimpleGlassPaneMsg();

            UIRegistry.displayLocalizedStatusBarText("SystemPrefs.CACHE_CLEARED");

            UIHelper.centerWindow(parentDlg);
        }
    };

    UIRegistry.writeSimpleGlassPaneMsg(getLocalizedMessage("SystemPrefs.CLEARING_CACHE"), 24);
    backupWorker.execute();
}

From source file:client.InterfaceJeu.java

public void selectionnerCarte(Component carte) {
    cartesSelectionnees.add(carte);/*from   w  w  w .j  a  v  a  2 s  . co m*/
    carte.setLocation(carte.getLocation().x, carte.getLocation().y - 10);
    verifierCartes();
}

From source file:client.InterfaceJeu.java

public void deselectionnerCarte(Component carte) {
    cartesSelectionnees.remove(carte);/*from  w w w  .  j  a v  a2s  .  com*/
    carte.setLocation(carte.getLocation().x, carte.getLocation().y + 10);
    verifierCartes();
}

From source file:edu.ku.brc.specify.utilapps.ERDVisualizer.java

public void calcLoc(final Point p, final Component src, final Component stop) {
    if (src != stop) {
        Point sp = src.getLocation();
        p.translate(sp.x, sp.y);/* w ww .ja  v  a  2s  .  c  o m*/
        calcLoc(p, src.getParent(), stop);
    }
}