Example usage for java.awt Component getLocationOnScreen

List of usage examples for java.awt Component getLocationOnScreen

Introduction

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

Prototype

public Point getLocationOnScreen() 

Source Link

Document

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

Usage

From source file:ComponentTree.java

/**
 * This main() method demonstrates the use of the ComponentTree class: it
 * puts a ComponentTree component in a Frame, and uses the ComponentTree to
 * display its own GUI hierarchy. It also adds a TreeSelectionListener to
 * display additional information about each component as it is selected
 *///  w w w .  ja va  2s.  c o  m
public static void main(String[] args) {
    // Create a frame for the demo, and handle window close requests
    JFrame frame = new JFrame("ComponentTree Demo");
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Create a scroll pane and a "message line" and add them to the
    // center and bottom of the frame.
    JScrollPane scrollpane = new JScrollPane();
    final JLabel msgline = new JLabel(" ");
    frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
    frame.getContentPane().add(msgline, BorderLayout.SOUTH);

    // Now create the ComponentTree object, specifying the frame as the
    // component whose tree is to be displayed. Also set the tree's font.
    JTree tree = new ComponentTree(frame);
    tree.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Only allow a single item in the tree to be selected at once
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Add an event listener for notifications when
    // the tree selection state changes.
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            // Tree selections are referred to by "path"
            // We only care about the last node in the path
            TreePath path = e.getPath();
            Component c = (Component) path.getLastPathComponent();
            // Now we know what component was selected, so
            // display some information about it in the message line
            if (c.isShowing()) {
                Point p = c.getLocationOnScreen();
                msgline.setText("x: " + p.x + "  y: " + p.y + "  width: " + c.getWidth() + "  height: "
                        + c.getHeight());
            } else {
                msgline.setText("component is not showing");
            }
        }
    });

    // Now that we've set up the tree, add it to the scrollpane
    scrollpane.setViewportView(tree);

    // Finally, set the size of the main window, and pop it up.
    frame.setSize(600, 400);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Returns component bounds on screen./*from   w  w w  .  j  av a  2s.co m*/
 *
 * @param component
 *            component to process
 * @return component bounds on screen
 */
public static Rectangle getBoundsOnScreen(final Component component) {
    return new Rectangle(component.getLocationOnScreen(), component.getSize());
}

From source file:Main.java

public static boolean isMouseOver(Component component) {
    return MouseInfo.getPointerInfo().getLocation().x >= component.getLocationOnScreen().x
            && MouseInfo.getPointerInfo().getLocation().x <= component.getLocationOnScreen().x
                    + component.getWidth()
            && MouseInfo.getPointerInfo().getLocation().y >= component.getLocationOnScreen().y
            && MouseInfo.getPointerInfo().getLocation().y <= component.getLocationOnScreen().y
                    + component.getHeight();
}

From source file:Main.java

/**
 * Returns the screen location for a component and X and Y.
 *///ww  w  .j  a  v a 2 s .  c o m
private static Point getScreenLocation(Component aComp, int anX, int aY) {
    Point point = aComp != null && aComp.isShowing() ? aComp.getLocationOnScreen() : new Point();
    point.x += anX;
    point.y += aY;
    return point;
}

From source file:Main.java

/**
 * Returns component location relative to another component.
 *
 * @param component  component to process
 * @param relativeTo component relative to which location will be returned
 * @return component location relative to another component
 *///from www  .  ja va  2s .  c  o m
public static Point getRelativeLocation(final Component component, final Component relativeTo) {
    final Point los = component.getLocationOnScreen();
    final Point rt = relativeTo.getLocationOnScreen();
    return new Point(los.x - rt.x, los.y - rt.y);
}

From source file:Main.java

public static void centerInParent(Window window, Component myParent) {
    int x;//from www .j a  v  a2 s.c  o  m
    int y;

    Point topLeft = myParent.getLocationOnScreen();
    Dimension parentSize = myParent.getSize();
    Dimension mySize = window.getSize();
    if (parentSize.width > mySize.width)
        x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
    else
        x = topLeft.x;

    if (parentSize.height > mySize.height)
        y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
    else
        y = topLeft.y;

    window.setLocation(x, y);
}

From source file:Main.java

public static Point getPopupPoint(Component parent, Component comp) {
    Dimension pSize = parent.getSize();
    Point loc = parent.getLocationOnScreen();

    loc.y += pSize.height;/*from   ww  w . ja  va2 s . com*/

    return loc;
}

From source file:Main.java

/**
 * Returns mouse point relative to specified component.
 *
 * @param component/*from   ww w.  ja  va  2 s. c  o  m*/
 *            component to process
 * @return mouse point relative to specified component
 */
public static Point getMousePoint(final Component component) {
    final Point p = MouseInfo.getPointerInfo().getLocation();
    final Point los = component.getLocationOnScreen();
    return new Point(p.x - los.x, p.y - los.y);
}

From source file:Main.java

public static Point getCenterPoint(Component parent, Component comp) {
    Dimension cSize = comp.getSize();
    Dimension pSize = parent.getSize();
    Point loc = parent.getLocationOnScreen();

    loc.x = ((pSize.width - cSize.width) / 2) + loc.x;
    loc.y = ((pSize.height - cSize.height) / 2) + loc.y;

    if (loc.x < 0) {
        loc.x = 0;//www.  ja v a2  s .  co  m
    }

    if (loc.y < 0) {
        loc.y = 0;
    }

    return loc;
}

From source file:Main.java

public static Point getRelLocation(Component c) {
    if (c == null || !c.isShowing()) {
        return new Point(0, 0);
    }// w w  w  . jav  a2  s.  co  m

    Container parent = getRootContainer(c);
    if ((parent != null) && parent.isShowing()) {
        Point p1 = c.getLocationOnScreen();
        Point p2 = parent.getLocationOnScreen();
        return new Point(p1.x - p2.x, p1.y - p2.y);
    }

    return new Point(0, 0);
}