Java Code Examples for java.awt.Component#getLocation()

The following examples show how to use java.awt.Component#getLocation() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public Dimension preferredLayoutSize(Container target) {
  synchronized (target.getTreeLock()) {
    Dimension dim = new Dimension(0, 0);

    for (int i = 0; i < target.getComponentCount(); i++) {
      Component m = target.getComponent(i);
      if (m.isVisible()) {
        Dimension d = m.getPreferredSize();

        // original
        // dim.height = Math.max(dim.height, d.height);
        // if (i > 0) { dim.width += hgap; }
        // dim.width += d.width;

        // new way
        Point p = m.getLocation();
        dim.width = Math.max(dim.width, p.x + d.width);
        dim.height = Math.max(dim.height, p.y + d.height);
      }
    }
    Insets insets = target.getInsets();
    dim.width += insets.left + insets.right + getHgap() * 2;
    dim.height += insets.top + insets.bottom + getVgap() * 2;
    return dim;
  }
}
 
Example 2
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 3
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 4
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 5
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 6
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 7
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 8
Source Project: JDKSourceCode1.8   File: MotifDesktopPaneUI.java    License: MIT License 6 votes vote down vote up
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 9
Source Project: jdk8u-jdk   File: DrawTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 10
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 11
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 12
Source Project: Ardulink-2   File: UtilityGeometry.java    License: Apache License 2.0 6 votes vote down vote up
public static void setAlignmentCentered(Component component, Component referredComponent) {
	if(referredComponent == null) {
		referredComponent = SwingUtilities.getRoot(component);
	}
	Point rootLocation = referredComponent.getLocation();
	Dimension rootDimension = referredComponent.getSize();
	Dimension componentDimension = component.getSize();
	
	Point componentLocation = new Point(rootLocation);
	int dx = (rootDimension.width - componentDimension.width) / 2;
	int dy = (rootDimension.height - componentDimension.height) / 2;
	componentLocation.translate(dx, dy);
	
	component.setLocation(componentLocation);
	
}
 
Example 13
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 14
@Override
public Dimension getPreferredSize() {
  Dimension size = super.getPreferredSize();
  if(getItemCount() <= maximumFastChoices && size.width > maximumWidth) {
    setSize(maximumWidth, Integer.MAX_VALUE);
    doLayout();
    int compCnt = getComponentCount();
    if(compCnt > 0) {
      Component lastComp = getComponent(compCnt - 1);
      Point compLoc = lastComp.getLocation();
      Dimension compSize = lastComp.getSize();
      size.width = maximumWidth;
      size.height = compLoc.y + compSize.height + getInsets().bottom;
    }
  }
  return size;
}
 
Example 15
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 16
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 17
Source Project: Bytecoder   File: MotifDesktopPaneUI.java    License: Apache License 2.0 6 votes vote down vote up
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 18
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 19
protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
    JInternalFrame.JDesktopIcon icon, int x, int y) {

    JInternalFrame.JDesktopIcon currentIcon = null;
    Component[] components = desktop.getComponents();

    for (int i=0; i<components.length; i++) {
        Component comp = components[i];
        if (comp instanceof JInternalFrame.JDesktopIcon &&
            comp != icon) {

            Point p = comp.getLocation();
            if (p.x == x && p.y == y) {
                return (JInternalFrame.JDesktopIcon)comp;
            }
        }
    }
    return null;
}
 
Example 20
Source Project: nextreports-designer   File: Show.java    License: Apache License 2.0 5 votes vote down vote up
public static void centrateComponent(Component parent, Component c) {
    if (parent == null) {
        centrateComponent(c);
    } else {
        Dimension dimParent = parent.getSize();
        Point p = parent.getLocation();
        Dimension dimComp = c.getSize();
        c.setLocation(p.x + ((dimParent.width - dimComp.width) / 2),
                p.y + ((dimParent.height - dimComp.height) / 2));
    }
}