Example usage for java.awt Container getLayout

List of usage examples for java.awt Container getLayout

Introduction

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

Prototype

public LayoutManager getLayout() 

Source Link

Document

Gets the layout manager for this container.

Usage

From source file:Main.java

public static Container updateLayout(Container container) {
    container.setLayout(container.getLayout());
    return container;
}

From source file:Main.java

protected static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component c = parent.getComponent(row * cols + col);
    return layout.getConstraints(c);
}

From source file:layout.BorderLayoutDemo.java

public static void addComponentsToPane(Container pane) {

    if (!(pane.getLayout() instanceof BorderLayout)) {
        pane.add(new JLabel("Container doesn't use BorderLayout!"));
        return;/*from  w  ww.  j  a v a 2 s  .  c  o m*/
    }

    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
    }

    JButton button = new JButton("Button 1 (PAGE_START)");
    pane.add(button, BorderLayout.PAGE_START);

    //Make the center component big, since that's the
    //typical usage of BorderLayout.
    button = new JButton("Button 2 (CENTER)");
    button.setPreferredSize(new Dimension(200, 100));
    pane.add(button, BorderLayout.CENTER);

    button = new JButton("Button 3 (LINE_START)");
    pane.add(button, BorderLayout.LINE_START);

    button = new JButton("Long-Named Button 4 (PAGE_END)");
    pane.add(button, BorderLayout.PAGE_END);

    button = new JButton("5 (LINE_END)");
    pane.add(button, BorderLayout.LINE_END);
}

From source file:SpringDemo4.java

public static void setContainerSize(Container parent, int pad) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component[] components = parent.getComponents();
    Spring maxHeightSpring = Spring.constant(0);
    SpringLayout.Constraints pCons = layout.getConstraints(parent);

    // Set the container's right edge to the right edge
    // of its rightmost component + padding.
    Component rightmost = components[components.length - 1];
    SpringLayout.Constraints rCons = layout.getConstraints(rightmost);
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(pad), rCons.getConstraint(SpringLayout.EAST)));

    // Set the container's bottom edge to the bottom edge
    // of its tallest component + padding.
    for (int i = 0; i < components.length; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(components[i]);
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getConstraint(SpringLayout.SOUTH));
    }//from w w  w  . j a v  a2s . com
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(pad), maxHeightSpring));
}

From source file:SpringDemo4.java

public static void setContainerSize(Container parent, int pad) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component[] components = parent.getComponents();
    Spring maxHeightSpring = Spring.constant(0);
    SpringLayout.Constraints pCons = layout.getConstraints(parent);

    //Set the container's right edge to the right edge
    //of its rightmost component + padding.
    Component rightmost = components[components.length - 1];
    SpringLayout.Constraints rCons = layout.getConstraints(rightmost);
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(pad), rCons.getConstraint(SpringLayout.EAST)));

    //Set the container's bottom edge to the bottom edge
    //of its tallest component + padding.
    for (int i = 0; i < components.length; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(components[i]);
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getConstraint(SpringLayout.SOUTH));
    }/*from   w w  w  .j  a v  a2  s . c o  m*/
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(pad), maxHeightSpring));
}

From source file:Main.java

/**
 * Set the anchors of each edge of a component to each edge of the passed in parent.</br>
 * The parent must have a {@link javax.swing.SpringLayout} as its {@link java.awt.LayoutManager}.</br>
 * </br>//from w  w w.ja  va 2  s  .  co m
 * Pass in -1 for any of the anchors and that edge will not be attached</br> 
 * Note that this method will remove all pre-existing anchors attached to item.
 * 
 * @param top
 */
public static void setSpringAnchors(int top, int right, int bottom, int left, Component comp,
        Container parent) {
    if (parent == null || comp == null) {
        throw new IllegalArgumentException("Parent and Component cannot be null");
    }
    if (!(parent.getLayout() instanceof SpringLayout)) {
        throw new IllegalArgumentException("Parent container does not have SpringLayout as its LayoutManager");
    }
    SpringLayout layout = (SpringLayout) parent.getLayout();
    layout.removeLayoutComponent(comp);
    if (top >= 0)
        layout.putConstraint(SpringLayout.NORTH, parent, top, SpringLayout.NORTH, comp);
    if (right >= 0)
        layout.putConstraint(SpringLayout.EAST, parent, right, SpringLayout.EAST, comp);
    if (bottom >= 0)
        layout.putConstraint(SpringLayout.SOUTH, parent, bottom, SpringLayout.SOUTH, comp);
    if (left >= 0)
        layout.putConstraint(SpringLayout.WEST, parent, left, SpringLayout.WEST, comp);
}

From source file:SpringBox.java

private static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component c = parent.getComponent(row * cols + col);
    return layout.getConstraints(c);
}

From source file:Main.java

/**
 * Puts the grid constraints for a component.
 * /*from w w w.  ja  va2s . co m*/
 * @param container
 *          The container to add the component to.
 * @param component
 *          The component to create the constraints for.
 * @param gridX
 *          The initial gridx value.
 * @param gridY
 *          The initial gridy value.
 * @param gridWidth
 *          The initial gridwidth value.
 * @param gridHeight
 *          The initial gridheight value.
 * @param weightX
 *          The initial weightx value.
 * @param weightY
 *          The initial weighty value.
 * @param anchor
 *          The initial anchor value.
 * @param fill
 *          The initial fill value.
 */
public static final void noInsetsPutGrid(final Container container, final Component component, final int gridX,
        final int gridY, final int gridWidth, final int gridHeight, final double weightX, final double weightY,
        final int anchor, final int fill) {
    LayoutManager l;
    l = container.getLayout();
    if (l instanceof GridBagLayout) {
        ((GridBagLayout) (l)).setConstraints(component, new GridBagConstraints(gridX, gridY, gridWidth,
                gridHeight, weightX, weightY, anchor, fill, NOINSETS, DEFAULT_IPAD_X, DEFAULT_IPAD_Y));
    }
    container.add(component);
}

From source file:Main.java

/**
 * Puts the grid constraints for a component.
 * //  w w  w  .  j a va  2 s. com
 * @param container
 *          The container to add the component to.
 * @param component
 *          The component to create the constraints for.
 * @param gridX
 *          The initial gridx value.
 * @param gridY
 *          The initial gridy value.
 * @param gridWidth
 *          The initial gridwidth value.
 * @param gridHeight
 *          The initial gridheight value.
 * @param weightX
 *          The initial weightx value.
 * @param weightY
 *          The initial weighty value.
 * @param anchor
 *          The initial anchor value.
 * @param fill
 *          The initial fill value.
 * @param insets
 *          The insets to be used.
 */
public static final void insetPutGrid(final Container container, final Component component, final int gridX,
        final int gridY, final int gridWidth, final int gridHeight, final double weightX, final double weightY,
        final int anchor, final int fill, final Insets insets) {
    LayoutManager l;
    l = container.getLayout();
    if (l instanceof GridBagLayout) {
        ((GridBagLayout) (l)).setConstraints(component, new GridBagConstraints(gridX, gridY, gridWidth,
                gridHeight, weightX, weightY, anchor, fill, insets, DEFAULT_IPAD_X, DEFAULT_IPAD_Y));
    }
    container.add(component);
}

From source file:Main.java

/**
 * Aligns the first <code>rows</code> <code>cols</code> components of <code>parent</code> in a grid. Each component in
 * a column is as wide as the maximum preferred width of the components in that column; height is similarly determined
 * for each row. The parent is made just big enough to fit them all.
 * //from   w  w  w.  j  av a  2 s  .  com
 * @param rows
 *          number of rows
 * @param cols
 *          number of columns
 * @param initialX
 *          x location to start the grid at
 * @param initialY
 *          y location to start the grid at
 * @param xPad
 *          x padding between cells
 * @param yPad
 *          y padding between cells
 */
public static void makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad,
        int yPad) {

    parent.setMaximumSize(new Dimension(Integer.MAX_VALUE, parent.getPreferredSize().height));
    SpringLayout layout;
    try {
        layout = (SpringLayout) parent.getLayout();
    } catch (ClassCastException exc) {
        System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
        return;
    }

    // Align all cells in each column and make them the same width.
    Spring x = Spring.constant(initialX);
    for (int c = 0; c < cols; c++) {
        Spring width = Spring.constant(0);
        for (int r = 0; r < rows; r++) {
            width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
        }
        for (int r = 0; r < rows; r++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setX(x);
            constraints.setWidth(width);
        }
        x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
    }

    // Align all cells in each row and make them the same height.
    Spring y = Spring.constant(initialY);
    for (int r = 0; r < rows; r++) {
        Spring height = Spring.constant(0);
        for (int c = 0; c < cols; c++) {
            height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
        }
        for (int c = 0; c < cols; c++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setY(y);
            constraints.setHeight(height);
        }
        y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
    }

    // Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH, y);
    pCons.setConstraint(SpringLayout.EAST, x);

}