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

The following examples show how to use java.awt.Component#setBounds() . 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
Source File: HorizontalLayout.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void layoutContainer(final Container parent) {
    final Insets insets = parent.getInsets();
    int posX = insets.left;
    final int posY = insets.top;
    final int height = parent.getHeight() - insets.top - insets.bottom;

    for (Component comp : parent.getComponents()) {
        if (comp.isVisible()) {
            Dimension pref = comp.getPreferredSize();
            if (proportionalHeight) {
                int h = Math.min(pref.height, height);
                int o = (height - h) / 2;
                comp.setBounds(posX, posY + o, pref.width, h);
            } else {
                comp.setBounds(posX, posY, pref.width, height);
            }
            posX += hGap;
            posX += pref.width;
        }
    }
}
 
Example 2
Source File: VerticalGridLayout.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void layoutContainer(Container parent) {
    int x = 0;
    int y = 0;
    int cellWidth = getMaxCellWidth();
    for (Component c : this.components) {
        if (c.isVisible()) {
            Dimension d = c.getPreferredSize();
            if (y + d.height > parent.getHeight()) {
                x += cellWidth;
                y = 0;
            }
            c.setBounds(x + 1, y + 1, cellWidth, d.height);
            y += d.height;
        }
    }
}
 
Example 3
Source File: CenterLayout.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Lays out the components.
 *
 * @param parent
 *          the parent.
 */
public void layoutContainer( final Container parent ) {

  synchronized ( parent.getTreeLock() ) {
    if ( parent.getComponentCount() > 0 ) {
      final Insets insets = parent.getInsets();
      final Dimension parentSize = parent.getSize();
      final Component component = parent.getComponent( 0 );
      final Dimension componentSize = component.getPreferredSize();
      final int xx =
          insets.left + ( Math.max( ( parentSize.width - insets.left - insets.right - componentSize.width ) / 2, 0 ) );
      final int yy =
          insets.top
              + ( Math.max( ( parentSize.height - insets.top - insets.bottom - componentSize.height ) / 2, 0 ) );
      component.setBounds( xx, yy, componentSize.width, componentSize.height );
    }
  }

}
 
Example 4
Source File: VerticalLayout.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void layoutContainer(final Container parent) {
    final Insets insets = parent.getInsets();
    final int posX = insets.left;
    int posY = insets.top;
    final int width = parent.getWidth() - insets.left - insets.right;

    for (Component comp : parent.getComponents()) {
        if (comp.isVisible()) {
            Dimension pref = comp.getPreferredSize();
            if (proportionalWidth) {
                int w = Math.min(pref.width, width);
                int o = (width - w) / 2;
                comp.setBounds(posX, posY + o, w, pref.height);
            } else {
                comp.setBounds(posX, posY, width, pref.height);
            }
            pref.height += vGap;
            posY += pref.height;
        }
    }
}
 
Example 5
Source File: SpringLayout.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void layoutContainer(Container parent) {
    setParent(parent);

    int n = parent.getComponentCount();
    getConstraints(parent).reset();
    for (int i = 0 ; i < n ; i++) {
        getConstraints(parent.getComponent(i)).reset();
    }

    Insets insets = parent.getInsets();
    Constraints pc = getConstraints(parent);
    abandonCycles(pc.getX()).setValue(0);
    abandonCycles(pc.getY()).setValue(0);
    abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
                                          insets.left - insets.right);
    abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
                                           insets.top - insets.bottom);

    for (int i = 0 ; i < n ; i++) {
        Component c = parent.getComponent(i);
        Constraints cc = getConstraints(c);
        int x = abandonCycles(cc.getX()).getValue();
        int y = abandonCycles(cc.getY()).getValue();
        int width = abandonCycles(cc.getWidth()).getValue();
        int height = abandonCycles(cc.getHeight()).getValue();
        c.setBounds(insets.left + x, insets.top + y, width, height);
    }
}
 
Example 6
Source File: RQueries.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void doLayout() {
    super.doLayout();
    Component c = getComponent(1);

    int h = c.getPreferredSize().height;
    Rectangle b = c.getBounds();

    b.y = (b.height - h) / 2 + 1;
    b.height = h;
    c.setBounds(b);
}
 
Example 7
Source File: SpringLayout.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void layoutContainer(Container parent) {
    setParent(parent);

    int n = parent.getComponentCount();
    getConstraints(parent).reset();
    for (int i = 0 ; i < n ; i++) {
        getConstraints(parent.getComponent(i)).reset();
    }

    Insets insets = parent.getInsets();
    Constraints pc = getConstraints(parent);
    abandonCycles(pc.getX()).setValue(0);
    abandonCycles(pc.getY()).setValue(0);
    abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
                                          insets.left - insets.right);
    abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
                                           insets.top - insets.bottom);

    for (int i = 0 ; i < n ; i++) {
        Component c = parent.getComponent(i);
        Constraints cc = getConstraints(c);
        int x = abandonCycles(cc.getX()).getValue();
        int y = abandonCycles(cc.getY()).getValue();
        int width = abandonCycles(cc.getWidth()).getValue();
        int height = abandonCycles(cc.getHeight()).getValue();
        c.setBounds(insets.left + x, insets.top + y, width, height);
    }
}
 
Example 8
Source File: PlaceholderPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void doLayout() {
    Component comp = getOccupant();
    if (comp != null) {
        comp.setBounds(0, 0, getWidth(), getHeight());
    }
}
 
Example 9
Source File: SpringLayout.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void layoutContainer(Container parent) {
    setParent(parent);

    int n = parent.getComponentCount();
    getConstraints(parent).reset();
    for (int i = 0 ; i < n ; i++) {
        getConstraints(parent.getComponent(i)).reset();
    }

    Insets insets = parent.getInsets();
    Constraints pc = getConstraints(parent);
    abandonCycles(pc.getX()).setValue(0);
    abandonCycles(pc.getY()).setValue(0);
    abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
                                          insets.left - insets.right);
    abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
                                           insets.top - insets.bottom);

    for (int i = 0 ; i < n ; i++) {
        Component c = parent.getComponent(i);
        Constraints cc = getConstraints(c);
        int x = abandonCycles(cc.getX()).getValue();
        int y = abandonCycles(cc.getY()).getValue();
        int width = abandonCycles(cc.getWidth()).getValue();
        int height = abandonCycles(cc.getHeight()).getValue();
        c.setBounds(insets.left + x, insets.top + y, width, height);
    }
}
 
Example 10
Source File: SpringLayout.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public void layoutContainer(Container parent) {
    setParent(parent);

    int n = parent.getComponentCount();
    getConstraints(parent).reset();
    for (int i = 0 ; i < n ; i++) {
        getConstraints(parent.getComponent(i)).reset();
    }

    Insets insets = parent.getInsets();
    Constraints pc = getConstraints(parent);
    abandonCycles(pc.getX()).setValue(0);
    abandonCycles(pc.getY()).setValue(0);
    abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
                                          insets.left - insets.right);
    abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
                                           insets.top - insets.bottom);

    for (int i = 0 ; i < n ; i++) {
        Component c = parent.getComponent(i);
        Constraints cc = getConstraints(c);
        int x = abandonCycles(cc.getX()).getValue();
        int y = abandonCycles(cc.getY()).getValue();
        int width = abandonCycles(cc.getWidth()).getValue();
        int height = abandonCycles(cc.getHeight()).getValue();
        c.setBounds(insets.left + x, insets.top + y, width, height);
    }
}
 
Example 11
Source File: RotatedPanel.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public void layoutContainer(Container parent) {
	Component child = parent.getComponent(0);
	Dimension d = new Dimension(parent.getWidth(), parent.getHeight());
	d = getRotation().transform(d);
	child.setBounds(new Rectangle(0, 0, d.width, d.height));
}
 
Example 12
Source File: TitledMenuSeparator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void doLayout() {
    super.doLayout();
    Component c = getComponent(1);
    
    int h = c.getPreferredSize().height;
    Rectangle b = c.getBounds();
    
    b.y = (b.height - h) / 2;
    b.height = h;
    c.setBounds(b);
}
 
Example 13
Source File: CompactLayout.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lays out the specified container.
 * @param parent the container to be laid out
 */
public void layoutContainer(Container parent) {
    int n = parent.getComponentCount();
    Insets insets = parent.getInsets();
    Dimension size = parent.getSize();
    int c = horizontal ? insets.left : insets.top;
    int x, y;
    int ebx = size.width - insets.right;
    size.width -= (insets.left + insets.right);
    size.height -= (insets.top + insets.bottom);
    for (int i = 0; i < n; i++) {
        Component comp = parent.getComponent(i);
        Dimension pref = comp.getPreferredSize();
        if (comp instanceof EnableButton) {
            ebx -= 4;
            ebx -= pref.width;
            x = ebx;
            y = (insets.top - pref.height) / 2;
        } else if (horizontal) {
            x = c;
            c += pref.width;
            y = insets.top;
            pref.height = size.height;
        } else {
            x = insets.left;
            pref.width = size.width;
            y = c;
            c += pref.height;
        }
        comp.setBounds(x, y, pref.width, pref.height);
    }
}
 
Example 14
Source File: SpringLayout.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void layoutContainer(Container parent) {
    setParent(parent);

    int n = parent.getComponentCount();
    getConstraints(parent).reset();
    for (int i = 0 ; i < n ; i++) {
        getConstraints(parent.getComponent(i)).reset();
    }

    Insets insets = parent.getInsets();
    Constraints pc = getConstraints(parent);
    abandonCycles(pc.getX()).setValue(0);
    abandonCycles(pc.getY()).setValue(0);
    abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
                                          insets.left - insets.right);
    abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
                                           insets.top - insets.bottom);

    for (int i = 0 ; i < n ; i++) {
        Component c = parent.getComponent(i);
        Constraints cc = getConstraints(c);
        int x = abandonCycles(cc.getX()).getValue();
        int y = abandonCycles(cc.getY()).getValue();
        int width = abandonCycles(cc.getWidth()).getValue();
        int height = abandonCycles(cc.getHeight()).getValue();
        c.setBounds(insets.left + x, insets.top + y, width, height);
    }
}
 
Example 15
Source File: SeaGlassTabbedPaneUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.apple.laf.AquaTabbedPaneCopyFromBasicUI$TabbedPaneLayout#layoutContainer(java.awt.Container)
 */
public void layoutContainer(Container parent) {
    setRolloverTab(-1);

    setScrollButtonDirections();
    calculateLayoutInfo();

    boolean shouldChangeFocus = verifyFocus(tabPane.getSelectedIndex());

    if (tabPane.getTabCount() <= 0) {
        return;
    }

    calcContentRect();
    
    for (int i = 0; i < tabPane.getComponentCount(); i++) {
        Component child = tabPane.getComponent(i);
        // Ignore the scroll buttons. They have already been positioned in
        // calculateTabRects, which will have been called by calculateLayoutInfo,
        // which is called above.
        if (child != scrollBackwardButton && child != scrollForwardButton) {
            child.setBounds(contentRect);
        }
    }
    setTabContainerBounds();
    layoutTabComponents();

    if (shouldChangeFocus && !SwingUtilities2.tabbedPaneChangeFocusTo(getVisibleComponent())) {
        tabPane.requestFocus();
    }
}
 
Example 16
Source File: Stacker.java    From beautyeye with Apache License 2.0 5 votes vote down vote up
@Override
public void doLayout() {
    // ensure all layers are sized the same
    Dimension size = getSize();
    Component layers[] = getComponents();
    for (Component layer : layers) {
        layer.setBounds(0, 0, size.width, size.height);
    }
}
 
Example 17
Source File: CompactLayout.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lays out the specified container.
 * @param parent the container to be laid out
 */
public void layoutContainer(Container parent) {
    int n = parent.getComponentCount();
    Insets insets = parent.getInsets();
    Dimension size = parent.getSize();
    int c = horizontal ? insets.left : insets.top;
    int x, y;
    int ebx = size.width - insets.right;
    size.width -= (insets.left + insets.right);
    size.height -= (insets.top + insets.bottom);
    for (int i = 0; i < n; i++) {
        Component comp = parent.getComponent(i);
        Dimension pref = comp.getPreferredSize();
        if (comp instanceof EnableButton) {
            ebx -= 4;
            ebx -= pref.width;
            x = ebx;
            y = (insets.top - pref.height) / 2;
        } else if (horizontal) {
            x = c;
            c += pref.width;
            y = insets.top;
            pref.height = size.height;
        } else {
            x = insets.left;
            pref.width = size.width;
            y = c;
            c += pref.height;
        }
        comp.setBounds(x, y, pref.width, pref.height);
    }
}
 
Example 18
Source File: PanelCellEditor.java    From erflute with Apache License 2.0 4 votes vote down vote up
protected static void addComponent(Container parent, Component component, int x, int y, int w, int h) {
    component.setFont(getAwtFont());
    component.setBounds(x, y, w, h);
    parent.add(component);
}
 
Example 19
Source File: AbstractListUI.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean redispatchComponent(MouseEvent e) {
    Point p = e.getPoint();
    int index = list.locationToIndex(p);
    if (index < 0 || index >= list.getModel().getSize()) {
        return false;
    }

    ListCellRenderer renderer = list.getCellRenderer();
    if (null == renderer) {
        return false;
    }
    Component renComponent = renderer.getListCellRendererComponent(list, list.getModel().getElementAt(index), index, false, false);
    if (null == renComponent) {
        return false;
    }
    Rectangle rect = list.getCellBounds(index, index);
    if (null == rect) {
        return false;
    }
    renComponent.setBounds(0, 0, rect.width, rect.height);
    renComponent.doLayout();
    Point p3 = rect.getLocation();

    Point p2 = new Point(p.x - p3.x, p.y - p3.y);
    Component dispatchComponent =
            SwingUtilities.getDeepestComponentAt(renComponent,
            p2.x, p2.y);
    if ( e.isPopupTrigger() &&
         dispatchComponent instanceof LinkButton && 
         !((LinkButton)dispatchComponent).isHandlingPopupEvents() ) 
    {
        return false;
    } 
    if (dispatchComponent instanceof AbstractButton) {
        if (!((AbstractButton) dispatchComponent).isEnabled()) {
            return false;
        }
        Point p4 = SwingUtilities.convertPoint(renComponent, p2, dispatchComponent);
        MouseEvent newEvent = new MouseEvent(dispatchComponent,
                e.getID(),
                e.getWhen(),
                e.getModifiers(),
                p4.x, p4.y,
                e.getClickCount(),
                e.isPopupTrigger(),
                MouseEvent.NOBUTTON);
        dispatchComponent.dispatchEvent(newEvent);
        list.repaint(rect);
        e.consume();
        return true;
    }
    return false;
}
 
Example 20
Source File: StackLayout.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License votes vote down vote up
public void layoutContainer(Container parent) {
        synchronized (parent.getTreeLock()) {
            int width = parent.getWidth();
            int height = parent.getHeight();

            Rectangle bounds = new Rectangle(0, 0, width, height);

            int componentsCount = components.size();
           
            for (int i = 0; i < componentsCount; i++) {
                Component comp = components.get(i);
                comp.setBounds(bounds);
                parent.setComponentZOrder(comp, componentsCount - i - 1);
            }
        }
    }