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:UsingComponentListener.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JCheckBox checkbox = new JCheckBox("Label visible", true);
    checkbox.addComponentListener(new ComponentListener() {
        public void componentHidden(ComponentEvent e) {
            System.out.println("componentHidden event from " + e.getComponent().getClass().getName());
        }//from   w  w w.  j a v a  2 s.  co  m

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

        public void componentResized(ComponentEvent e) {
            Component c = e.getComponent();
            System.out.println("componentResized event from " + c.getClass().getName() + "; new size: "
                    + c.getSize().width + ", " + c.getSize().height);
        }

        public void componentShown(ComponentEvent e) {
            System.out.println("componentShown event from " + e.getComponent().getClass().getName());
        }

    });

    frame.add(checkbox, "North");

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * <p>//from w ww  .  j  a  v a 2s. c o  m
 * getCenter.
 * </p>
 *
 * @param component
 *            a {@link java.awt.Component} object.
 * @return a {@link java.awt.Point} object.
 */
public static Point getCenter(final Component component) {
    final Point point = component.getLocation();
    point.translate( //
            (int) Math.floor((component.getWidth() + 0.5) / 2.0), //
            (int) Math.floor((component.getHeight() + 0.5) / 2.0));
    return point;
}

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;//  www.j a v a 2  s  . 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

static public void fitComponentIntoScreen(Component component) {
    fitComponentIntoScreen(component, component.getLocation());
}

From source file:net.chaosserver.timelord.swingui.SwingUtil.java

/**
 * Repair location is designed to detect if a box is partially
 * off-screen and move the box back onto the screen.
 *
 * @param component component to repair//from   w  w w  . ja v  a 2 s .c  om
 */
public static void repairLocation(Component component) {
    Point locationPoint = component.getLocation();
    Point locationOnScreenPoint = null;
    if (component.isVisible()) {
        locationOnScreenPoint = component.getLocationOnScreen();
    }
    Dimension componentSize = component.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    if (log.isDebugEnabled()) {
        log.debug("Repairing location on [" + component.getClass().getName() + "].  Original location point = ["
                + locationPoint + "] and location on screen point = [" + locationOnScreenPoint
                + "].  The screen size is [" + screenSize + "] and the component size is [" + componentSize
                + "]");
    }

    // Is the dialog to far to the left?  Then fix.
    if (locationPoint.getX() < 0) {
        locationPoint.setLocation(0, locationPoint.getY());
    }
    if (locationPoint.getY() < 0) {
        locationPoint.setLocation(locationPoint.getX(), 0);
    }
    // component.setLocation(locationPoint);

    // Is the dialog too wide?
    if (locationPoint.getX() + componentSize.getWidth() > screenSize.getWidth()) {

        componentSize.width = (int) (screenSize.getWidth() - locationPoint.getX());
    }
    if (locationPoint.getY() + componentSize.getHeight() > screenSize.getHeight()) {

        componentSize.height = (int) (screenSize.getHeight() - locationPoint.getY());
    }

    // component.setSize(componentSize);
}

From source file:Main.java

public static void setCenter(Component component, Component component1) {
    Point point;/*www. j a v a2s .c  om*/
    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:Main.java

/**
 * Calculates the position of a frame to be in the center of an other frame.
 *
 * @param parentFrame//from w  ww.j  a  v a  2 s  . c  om
 * @param frame
 * @return
 */
public static Point getCenter(final Component parentFrame, final Window frame) {
    final Point point = new Point();
    int x = 0, y = 0;

    if (parentFrame == null || frame == null) {
        point.setLocation(x, y);
        return point;
    }

    x = parentFrame.getLocation().x + parentFrame.getSize().width / 2 - frame.getSize().width / 2;
    y = parentFrame.getLocation().y + parentFrame.getSize().height / 2 - frame.getSize().height / 2;

    point.setLocation(x, y);

    return point;
}

From source file:Main.java

static public void overlapComponents(Component top, Component bottom, int offset,
        boolean preserveZeroLocation) {
    ///*from   w w w  .  j  a  v a 2 s  .  com*/
    // If top's location is (0,0), then adjust the location so that
    // the first appearance will be on the top of the parent frame.
    //
    Point location = top.getLocation();

    //
    // if preserveZeroLocation is true, it means that location(0,0) is
    // a valid location. [V1.75]
    //
    if (location.x == 0 && location.y == 0 && !preserveZeroLocation) {
        //
        // With Windows 95, if the main frame is iconified, its location()
        // might be out of screen. Therefore, if the its location is out
        // of screen, then position the top component into the center 
        // of the screen.
        //
        location = bottom.getLocation();

        if ((location.x > SCREEN_SIZE.width) || (location.y > SCREEN_SIZE.height))
            centerComponent(top, new Point(0, 0), SCREEN_SIZE);
        else {
            location.x += offset;
            location.y += offset;
            fitComponentIntoScreen(top, location);
        }
    } else
        fitComponentIntoScreen(top, location);
}

From source file:Main.java

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

    Dimension othersDimension = null;
    Point othersLocation = null;/*from w  ww.ja va  2 s .c  o  m*/
    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 main() {

    ComponentListener listener = new ComponentAdapter() {
        public void componentShown(ComponentEvent evt) {
            Component c = (Component) evt.getSource();
            System.out.println("Component is now visible");
        }/*  w  w w . j a  v a 2s  . c o m*/

        public void componentHidden(ComponentEvent evt) {
            Component c = (Component) evt.getSource();
            System.out.println("Component is now hidden");
        }

        public void componentMoved(ComponentEvent evt) {
            Component c = (Component) evt.getSource();

            Point newLoc = c.getLocation();
            System.out.println("Get new location");
        }

        public void componentResized(ComponentEvent evt) {
            Component c = (Component) evt.getSource();

            Dimension newSize = c.getSize();
            System.out.println("Get new size");
        }
    };
    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    frame.addComponentListener(listener);
    frame.setVisible(true);
}