Example usage for java.awt Component getX

List of usage examples for java.awt Component getX

Introduction

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

Prototype

public int getX() 

Source Link

Document

Returns the current x coordinate of the components origin.

Usage

From source file:Util.java

public static final Component getVisibleChildAt(Container container, Point p) {
    for (int i = 0; i < container.getComponentCount(); i++) {
        Component c = container.getComponent(i);
        if (c.isVisible() && c.contains(p.x - c.getX(), p.y - c.getY()))
            return c;
    }/*from   w  ww . j  av a  2 s . c o m*/

    return null;
}

From source file:Main.java

/**
 * Use this static method if you want to center a component over another
 * component./* w  ww .  ja va  2s .  co m*/
 *
 * @param parent
 *            the component you want to use to place it on
 * @param toBeCentered
 *            the component you want to center
 */
public static void centerComponentInComponent(Component parent, Component toBeCentered) {
    toBeCentered.setLocation(parent.getX() + (parent.getWidth() - toBeCentered.getWidth()) / 2,
            parent.getY() + (parent.getHeight() - toBeCentered.getHeight()) / 2);

    toBeCentered.validate();
    toBeCentered.repaint();
}

From source file:Main.java

public static final Component getChildAtLine(Container container, Point p, boolean horizontal) {
    if (horizontal) {
        for (int i = 0; i < container.getComponentCount(); i++) {
            Component c = container.getComponent(i);
            if (p.x >= c.getX() && p.x < c.getX() + c.getWidth())
                return c;
        }//  ww  w  .ja  v a  2s .co m
    } else {
        for (int i = 0; i < container.getComponentCount(); i++) {
            Component c = container.getComponent(i);
            if (p.y >= c.getY() && p.y < c.getY() + c.getHeight())
                return c;
        }
    }

    return null;
}

From source file:Main.java

public static void scrollRectToVisible(Component component, Rectangle aRect) {
    Container parent;//from  w  w  w  .  j  a v a 2s . co  m
    int dx = component.getX(), dy = component.getY();

    for (parent = component.getParent(); parent != null
            && (!(parent instanceof JViewport) || (((JViewport) parent)
                    .getClientProperty("HierarchicalTable.mainViewport") == null)); parent = parent
                            .getParent()) {
        Rectangle bounds = parent.getBounds();

        dx += bounds.x;
        dy += bounds.y;
    }

    if (parent != null) {
        aRect.x += dx;
        aRect.y += dy;

        ((JComponent) parent).scrollRectToVisible(aRect);
        aRect.x -= dx;
        aRect.y -= dy;
    }
}

From source file:Main.java

public static int getX(final Component component) {
    if (component != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            return component.getX();
        } else {//w  w w. ja  v  a 2  s .  c o  m
            final int[] x = new int[1];

            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        x[0] = component.getX();
                    }
                });
            } catch (InterruptedException | InvocationTargetException e) {

            }

            return x[0];
        }
    } else {
        return 0;
    }
}

From source file:Main.java

public static int getX(final Component component) {
    return component != null ? runInEDT(() -> Integer.valueOf(component.getX())).intValue() : 0;
}

From source file:Main.java

public static Rectangle getRelativeBounds(Component component) {
    Rectangle bounds = new Rectangle(0, 0, component.getWidth(), component.getHeight());
    Container parent = component.getParent();

    while (parent != null) {
        bounds.x += component.getX();
        bounds.y += component.getY();/*  ww  w  .  ja  va  2 s.c  o m*/
        component = parent;
        parent = component.getParent();
    }

    return bounds;
}

From source file:Main.java

private static java.awt.Point getLocation(java.awt.Point rv, java.awt.Component c,
        java.awt.Component ancestor) {
    assert c != null;
    if (c == ancestor) {
        return rv;
    } else {//from w w w  . j a v  a2 s. c o  m
        rv.x += c.getX();
        rv.y += c.getY();
        return getLocation(rv, c.getParent(), ancestor);
    }
}

From source file:Main.java

/**
 * Centra una finestra all'interno di un'altra
 *
 * @param f componente target (finestra esterna) - se null centra nello
 * schermo//www .  j  a va  2 s  . c o  m
 * @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:labr_client.xml.ObjToXML.java

public static void saveProfile(Component[] comps, String profile) {

    try {//from  w ww . j a va2  s  . c  o m
        if (comps != null) {
            JAXBContext context = JAXBContext.newInstance(LabrRequest.class);
            Marshaller m = context.createMarshaller();
            //for pretty-print XML in JAXB
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            LabrRequest request = new LabrRequest();
            for (Component comp : comps) {

                if (comp.getName() != null) {

                    if (comp.getName().equals("name")) {
                        request.patient.name.setValue(((JTextField) comp).getText());
                        request.patient.name.setX(comp.getX());
                        request.patient.name.setY(comp.getY());
                        request.patient.name.setWidth(comp.getWidth());
                    } else if (comp.getName().equals("firstName")) {
                        request.patient.firstName.setValue(((JTextField) comp).getText());
                        request.patient.firstName.setX(comp.getX());
                        request.patient.firstName.setY(comp.getY());
                        request.patient.firstName.setWidth(comp.getWidth());
                    } else if (comp.getName().equals("birthDate")) {
                        request.patient.birthDate.setValue(((JFormattedTextField) comp).getText());
                        request.patient.birthDate.setX(comp.getX());
                        request.patient.birthDate.setY(comp.getY());
                        request.patient.birthDate.setWidth(comp.getWidth());
                    } else if (comp.getName().equals("gender")) {
                        request.patient.gender.setValue((String) (((JComboBox) comp).getSelectedItem()));
                        request.patient.gender.setX(comp.getX());
                        request.patient.gender.setY(comp.getY());
                    } else if (comp.getName().equals("straatAndNumber")) {
                        request.patient.straatAndNumber.setValue(((JTextField) comp).getText());
                        request.patient.straatAndNumber.setX(comp.getX());
                        request.patient.straatAndNumber.setY(comp.getY());
                        request.patient.straatAndNumber.setWidth(comp.getWidth());
                    } else if (comp.getName().equals("zip")) {
                        request.patient.zip.setValue(((JTextField) comp).getText());
                        request.patient.zip.setX(comp.getX());
                        request.patient.zip.setY(comp.getY());
                        request.patient.zip.setWidth(comp.getWidth());
                    } else if (comp.getName().equals("city")) {
                        request.patient.city.setValue(((JTextField) comp).getText());
                        request.patient.city.setX(comp.getX());
                        request.patient.city.setY(comp.getY());
                        request.patient.city.setWidth(comp.getWidth());
                    } else if (comp.getName().equals("country")) {
                        request.patient.country.setValue((String) (((JComboBox) comp).getSelectedItem()));
                        request.patient.country.setX(comp.getX());
                        request.patient.country.setY(comp.getY());
                    } else if (comp.getName().equals("nationalNumber")) {
                        request.patient.nationalNumber.setValue(((JTextField) comp).getText());
                        request.patient.nationalNumber.setX(comp.getX());
                        request.patient.nationalNumber.setY(comp.getY());
                        request.patient.nationalNumber.setWidth(comp.getWidth());
                    } else if (comp.getName().equals("Save")) {
                        JButton jbut = (JButton) comp;
                        ImageIcon icon = (ImageIcon) jbut.getIcon();
                        request.buttons.save.setX(comp.getX());
                        request.buttons.save.setY(comp.getY());
                        request.buttons.save.setValue("Save");
                        if (icon != null) {
                            request.buttons.save.setIcon(icon.getDescription());
                        }
                    } else if (comp.getName().equals("Search")) {
                    } else if (comp.getName().equals("saveAndSend")) {
                        request.patient.nationalNumber.setValue(((JTextField) comp).getText());
                        request.patient.nationalNumber.setX(comp.getX());
                        request.patient.nationalNumber.setY(comp.getY());
                        request.patient.nationalNumber.setWidth(comp.getWidth());
                    } else if (comp.getName().equals("print")) {
                        request.patient.nationalNumber.setValue(((JTextField) comp).getText());
                        request.patient.nationalNumber.setX(comp.getX());
                        request.patient.nationalNumber.setY(comp.getY());
                        request.patient.nationalNumber.setWidth(comp.getWidth());
                    } else {
                        Class<? extends Component> c = comp.getClass();
                        if (c.getSimpleName().equals("JLabel")) {
                            JLabel lbl = (JLabel) comp;
                            LabrXMLLabel l = new LabrXMLLabel();
                            l.setColor(String.valueOf(lbl.getForeground().getRGB()));
                            l.setSize(lbl.getFont().getSize());
                            l.setId(lbl.getName());
                            l.setValue(lbl.getText());
                            l.setX(lbl.getX());
                            l.setY(lbl.getY());
                            request.labels.getLabel().add(l);
                        }
                        ;
                        if (c.getSimpleName().equals("JCheckBox")) {
                            JCheckBox chbx = (JCheckBox) comp;
                            LabrXMLRequest req = new LabrXMLRequest();
                            req.setX(chbx.getX());
                            req.setY(chbx.getY());
                            req.setLoinc(chbx.getName());
                            req.setValue(chbx.getText());
                            req.setSelected(chbx.isSelected());
                            request.requests.getRequest().add(req);
                        }
                        ;
                        if (c.getSimpleName().equals("JTextBox")) {

                        }
                        ;
                    }
                }

            }
            m.marshal(request, new File(PublicVars.getUserData()[9] + "\\" + profile + ".xml"));
        }
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}