Example usage for java.awt Container getBounds

List of usage examples for java.awt Container getBounds

Introduction

In this page you can find the example usage for java.awt Container getBounds.

Prototype

public Rectangle getBounds() 

Source Link

Document

Gets the bounds of this component in the form of a Rectangle object.

Usage

From source file:Main.java

/**
 * Centers a component on its parent./*  www . ja  v  a  2 s. c  o  m*/
 *
 * @param component
 */
public static void centerInParent(Component component) {
    Container parent = component.getParent();
    if (parent != null) {
        Rectangle parentBounds = parent.getBounds();
        Rectangle dialogBounds = new Rectangle(
                (int) (parentBounds.getMinX() + parentBounds.getWidth() / 2 - component.getWidth() / 2),
                (int) (parentBounds.getMinY() + parentBounds.getHeight() / 2 - component.getHeight() / 2),
                component.getWidth(), component.getHeight());
        //dialog.setBounds( dialogBounds );
        component.setLocation(dialogBounds.x, dialogBounds.y);
    }
}

From source file:Main.java

public static void scrollRectToVisible(Component component, Rectangle aRect) {
    Container parent;
    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;/*www.  jav a2 s  .c  om*/
        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:OverlayLayout.java

/**
 * Lays out the specified container.//  w  w w.j  a va2 s . c o m
 *
 * @param parent the container to be laid out
 */
public void layoutContainer(final Container parent) {
    synchronized (parent.getTreeLock()) {
        final Insets ins = parent.getInsets();

        final Rectangle bounds = parent.getBounds();
        final int width = bounds.width - ins.left - ins.right;
        final int height = bounds.height - ins.top - ins.bottom;

        final Component[] comps = parent.getComponents();

        for (int i = 0; i < comps.length; i++) {
            final Component c = comps[i];
            if ((comps[i].isVisible() == false) && this.ignoreInvisible) {
                continue;
            }
            c.setBounds(ins.left, ins.top, width, height);
        }
    }
}

From source file:com.all.browser.impl.MozillaBrowser.java

private void registerListeners() {
    mozillaPanel.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
        @Override/*from w  ww .java 2  s.c  o m*/
        public void ancestorResized(HierarchyEvent e) {
            Container changedParent = e.getChangedParent();
            if (changedParent != null) {
                mozillaPanel.setBounds(changedParent.getBounds());
            }
        }
    });

    mozillaPanel.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_SPACE) {
                e.consume();
            }
        }
    });
}

From source file:FormatLayout.java

/**
 * Performs the layout of the container.
 *
 * @param parent  the parent.//from w w  w. j a v a  2 s .c o m
 */
public void layoutContainer(final Container parent) {
    Component c0, c1, c2, c3, c4, c5;

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        int componentIndex = 0;
        final int rowCount = this.rowHeights.length;
        for (int i = 0; i < this.columnWidths.length; i++) {
            this.columnWidths[i] = 0;
        }
        this.columns1and2Width = 0;
        this.columns4and5Width = 0;
        this.columns1to4Width = 0;
        this.columns1to5Width = 0;
        this.columns0to5Width = parent.getBounds().width - insets.left - insets.right;

        this.totalHeight = 0;
        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
            final int format = this.rowFormats[rowIndex % this.rowFormats.length];
            switch (format) {
            case FormatLayout.C:
                c0 = parent.getComponent(componentIndex);
                updateC(rowIndex, c0.getPreferredSize());
                componentIndex = componentIndex + 1;
                break;
            case FormatLayout.LC:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                updateLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize());
                componentIndex = componentIndex + 2;
                break;
            case FormatLayout.LCB:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                updateLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize());
                componentIndex = componentIndex + 3;
                break;
            case FormatLayout.LCLC:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                c3 = parent.getComponent(componentIndex + 3);
                updateLCLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(),
                        c3.getPreferredSize());
                componentIndex = componentIndex + 4;
                break;
            case FormatLayout.LCBLC:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                c3 = parent.getComponent(componentIndex + 3);
                c4 = parent.getComponent(componentIndex + 4);
                updateLCBLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(),
                        c3.getPreferredSize(), c4.getPreferredSize());
                componentIndex = componentIndex + 5;
                break;
            case FormatLayout.LCLCB:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                c3 = parent.getComponent(componentIndex + 3);
                c4 = parent.getComponent(componentIndex + 4);
                updateLCLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(),
                        c3.getPreferredSize(), c4.getPreferredSize());
                componentIndex = componentIndex + 5;
                break;
            case FormatLayout.LCBLCB:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                c3 = parent.getComponent(componentIndex + 3);
                c4 = parent.getComponent(componentIndex + 4);
                c5 = parent.getComponent(componentIndex + 5);
                updateLCBLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(),
                        c3.getPreferredSize(), c4.getPreferredSize(), c5.getPreferredSize());
                componentIndex = componentIndex + 6;
                break;
            }
        }
        complete();

        componentIndex = 0;
        int rowY = insets.top;
        final int[] rowX = new int[6];
        rowX[0] = insets.left;
        rowX[1] = rowX[0] + this.columnWidths[0] + this.columnGaps[0];
        rowX[2] = rowX[1] + this.columnWidths[1] + this.columnGaps[1];
        rowX[3] = rowX[2] + this.columnWidths[2] + this.columnGaps[2];
        rowX[4] = rowX[3] + this.columnWidths[3] + this.columnGaps[3];
        rowX[5] = rowX[4] + this.columnWidths[4] + this.columnGaps[4];
        final int w1to2 = this.columnWidths[1] + this.columnGaps[1] + this.columnWidths[2];
        final int w4to5 = this.columnWidths[4] + this.columnGaps[4] + this.columnWidths[5];
        final int w1to4 = w1to2 + this.columnGaps[2] + this.columnWidths[3] + this.columnGaps[3]
                + this.columnWidths[4];
        final int w1to5 = w1to4 + this.columnGaps[4] + this.columnWidths[5];
        final int w0to5 = w1to5 + this.columnWidths[0] + this.columnGaps[0];
        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
            final int format = this.rowFormats[rowIndex % this.rowFormats.length];

            switch (format) {
            case FormatLayout.C:
                c0 = parent.getComponent(componentIndex);
                c0.setBounds(rowX[0], rowY, w0to5, c0.getPreferredSize().height);
                componentIndex = componentIndex + 1;
                break;
            case FormatLayout.LC:
                c0 = parent.getComponent(componentIndex);
                c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2,
                        this.columnWidths[0], c0.getPreferredSize().height);
                c1 = parent.getComponent(componentIndex + 1);
                c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2,
                        w1to5, c1.getPreferredSize().height);
                componentIndex = componentIndex + 2;
                break;
            case FormatLayout.LCB:
                c0 = parent.getComponent(componentIndex);
                c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2,
                        this.columnWidths[0], c0.getPreferredSize().height);
                c1 = parent.getComponent(componentIndex + 1);
                c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2,
                        w1to4, c1.getPreferredSize().height);
                c2 = parent.getComponent(componentIndex + 2);
                c2.setBounds(rowX[5], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2,
                        this.columnWidths[5], c2.getPreferredSize().height);
                componentIndex = componentIndex + 3;
                break;
            case FormatLayout.LCLC:
                c0 = parent.getComponent(componentIndex);
                c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2,
                        this.columnWidths[0], c0.getPreferredSize().height);
                c1 = parent.getComponent(componentIndex + 1);
                c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2,
                        w1to2, c1.getPreferredSize().height);
                c2 = parent.getComponent(componentIndex + 2);
                c2.setBounds(rowX[3], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2,
                        this.columnWidths[3], c2.getPreferredSize().height);
                c3 = parent.getComponent(componentIndex + 3);
                c3.setBounds(rowX[4], rowY + (this.rowHeights[rowIndex] - c3.getPreferredSize().height) / 2,
                        w4to5, c3.getPreferredSize().height);
                componentIndex = componentIndex + 4;
                break;
            case FormatLayout.LCBLC:
                c0 = parent.getComponent(componentIndex);
                c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2,
                        this.columnWidths[0], c0.getPreferredSize().height);
                c1 = parent.getComponent(componentIndex + 1);
                c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2,
                        this.columnWidths[1], c1.getPreferredSize().height);
                c2 = parent.getComponent(componentIndex + 2);
                c2.setBounds(rowX[2], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2,
                        this.columnWidths[2], c2.getPreferredSize().height);
                c3 = parent.getComponent(componentIndex + 3);
                c3.setBounds(rowX[3], rowY + (this.rowHeights[rowIndex] - c3.getPreferredSize().height) / 2,
                        this.columnWidths[3], c3.getPreferredSize().height);
                c4 = parent.getComponent(componentIndex + 4);
                c4.setBounds(rowX[4], rowY + (this.rowHeights[rowIndex] - c4.getPreferredSize().height) / 2,
                        w4to5, c4.getPreferredSize().height);
                componentIndex = componentIndex + 5;
                break;
            case FormatLayout.LCLCB:
                c0 = parent.getComponent(componentIndex);
                c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2,
                        this.columnWidths[0], c0.getPreferredSize().height);
                c1 = parent.getComponent(componentIndex + 1);
                c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2,
                        w1to2, c1.getPreferredSize().height);
                c2 = parent.getComponent(componentIndex + 2);
                c2.setBounds(rowX[3], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2,
                        this.columnWidths[3], c2.getPreferredSize().height);
                c3 = parent.getComponent(componentIndex + 3);
                c3.setBounds(rowX[4], rowY + (this.rowHeights[rowIndex] - c3.getPreferredSize().height) / 2,
                        this.columnWidths[4], c3.getPreferredSize().height);
                c4 = parent.getComponent(componentIndex + 4);
                c4.setBounds(rowX[5], rowY + (this.rowHeights[rowIndex] - c4.getPreferredSize().height) / 2,
                        this.columnWidths[5], c4.getPreferredSize().height);
                componentIndex = componentIndex + 5;
                break;
            case FormatLayout.LCBLCB:
                c0 = parent.getComponent(componentIndex);
                c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2,
                        this.columnWidths[0], c0.getPreferredSize().height);
                c1 = parent.getComponent(componentIndex + 1);
                c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2,
                        this.columnWidths[1], c1.getPreferredSize().height);
                c2 = parent.getComponent(componentIndex + 2);
                c2.setBounds(rowX[2], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2,
                        this.columnWidths[2], c2.getPreferredSize().height);
                c3 = parent.getComponent(componentIndex + 3);
                c3.setBounds(rowX[3], rowY + (this.rowHeights[rowIndex] - c3.getPreferredSize().height) / 2,
                        this.columnWidths[3], c3.getPreferredSize().height);
                c4 = parent.getComponent(componentIndex + 4);
                c4.setBounds(rowX[4], rowY + (this.rowHeights[rowIndex] - c4.getPreferredSize().height) / 2,
                        this.columnWidths[4], c4.getPreferredSize().height);
                c5 = parent.getComponent(componentIndex + 5);
                c5.setBounds(rowX[5], rowY + (this.rowHeights[rowIndex] - c5.getPreferredSize().height) / 2,
                        this.columnWidths[5], c5.getPreferredSize().height);
                componentIndex = componentIndex + 6;
                break;
            }
            rowY = rowY + this.rowHeights[rowIndex] + this.rowGap;
        }
    }
}

From source file:org.eclipse.wb.internal.swing.MigLayout.model.MigDimensionInfo.java

/**
 * @return the size of single unit in pixels.
 *//*w  w w  .j  a  va2s .  c o  m*/
public float getPixelsInOneUnit(String unit) {
    UnitValue unitValue = ConstraintParser.parseUnitValue("1" + unit, m_horizontal);
    Container container = m_layout.getContainer().getContainer();
    SwingContainerWrapper parentWrapper = new SwingContainerWrapper(container);
    int refValue = m_horizontal ? container.getBounds().width : container.getBounds().height;
    return unitValue.getPixelsExact(refValue, parentWrapper, null);
}

From source file:org.eclipse.wb.internal.swing.utils.SwingImageUtils.java

private static void fetchMenuVisualData_JMenuBar(MenuVisualData menuData, Container menuObject,
        Container parent) throws Exception {
    if (parent != null) {
        menuData.m_menuBounds = CoordinateUtils
                .get(SwingUtilities.convertRectangle(menuObject, menuObject.getBounds(), parent));
    } else {//from   ww w .j av  a  2  s.c  o m
        // image
        {
            JFrame frame = new JFrame();
            frame.setBounds(menuObject.getBounds());
            frame.setJMenuBar((JMenuBar) menuObject);
            frame.pack();
            prepareForPrinting(frame);
            try {
                menuData.m_menuImage = createComponentShot(menuObject);
            } finally {
                setVisible(frame, false);
                frame.dispose();
            }
        }
        // bounds
        menuData.m_menuBounds = CoordinateUtils.get(menuObject.getBounds());
    }
    // items
    fetchMenuVisualData_items(menuData, menuObject);
}