Exemplo n.º 1
0
  /**
   * Determines the minimum size of the container argument using this grid layout.
   *
   * <p>The minimum width of a grid layout is the largest minimum width of all of the components in
   * the container times the number of columns, plus the horizontal padding times the number of
   * columns minus one, plus the left and right insets of the target container.
   *
   * <p>The minimum height of a grid layout is the largest minimum height of all of the components
   * in the container times the number of rows, plus the vertical padding times the number of rows
   * minus one, plus the top and bottom insets of the target container.
   *
   * @param parent the container in which to do the layout
   * @return the minimum dimensions needed to lay out the subcomponents of the specified container
   * @see java.awt.GridLayout#preferredLayoutSize
   * @see java.awt.Container#doLayout
   */
  public Dimension minimumLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
      Insets insets = parent.getInsets();
      int ncomponents = parent.getComponentCount();
      int nrows = rows;
      int ncols = cols;

      if (nrows > 0) {
        ncols = (ncomponents + nrows - 1) / nrows;
      } else {
        nrows = (ncomponents + ncols - 1) / ncols;
      }
      int w = 0;
      int h = 0;
      for (int i = 0; i < ncomponents; i++) {
        Component comp = parent.getComponent(i);
        Dimension d = comp.getMinimumSize();
        if (w < d.width) {
          w = d.width;
        }
        if (h < d.height) {
          h = d.height;
        }
      }
      return new Dimension(
          insets.left + insets.right + ncols * w + (ncols - 1) * hgap,
          insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap);
    }
  }
Exemplo n.º 2
0
  /**
   * Lays out the specified container using this layout.
   *
   * <p>This method reshapes the components in the specified target container in order to satisfy
   * the constraints of the <code>GridLayout</code> object.
   *
   * <p>The grid layout manager determines the size of individual components by dividing the free
   * space in the container into equal-sized portions according to the number of rows and columns in
   * the layout. The container's free space equals the container's size minus any insets and any
   * specified horizontal or vertical gap. All components in a grid layout are given the same size.
   *
   * @param parent the container in which to do the layout
   * @see java.awt.Container
   * @see java.awt.Container#doLayout
   */
  public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
      Insets insets = parent.getInsets();
      int ncomponents = parent.getComponentCount();
      int nrows = rows;
      int ncols = cols;
      boolean ltr = parent.getComponentOrientation().isLeftToRight();

      if (ncomponents == 0) {
        return;
      }
      if (nrows > 0) {
        ncols = (ncomponents + nrows - 1) / nrows;
      } else {
        nrows = (ncomponents + ncols - 1) / ncols;
      }
      // 4370316. To position components in the center we should:
      // 1. get an amount of extra space within Container
      // 2. incorporate half of that value to the left/top position
      // Note that we use trancating division for widthOnComponent
      // The reminder goes to extraWidthAvailable
      int totalGapsWidth = (ncols - 1) * hgap;
      int widthWOInsets = parent.width - (insets.left + insets.right);
      int widthOnComponent = (widthWOInsets - totalGapsWidth) / ncols;
      int extraWidthAvailable = (widthWOInsets - (widthOnComponent * ncols + totalGapsWidth)) / 2;

      int totalGapsHeight = (nrows - 1) * vgap;
      int heightWOInsets = parent.height - (insets.top + insets.bottom);
      int heightOnComponent = (heightWOInsets - totalGapsHeight) / nrows;
      int extraHeightAvailable =
          (heightWOInsets - (heightOnComponent * nrows + totalGapsHeight)) / 2;
      if (ltr) {
        for (int c = 0, x = insets.left + extraWidthAvailable;
            c < ncols;
            c++, x += widthOnComponent + hgap) {
          for (int r = 0, y = insets.top + extraHeightAvailable;
              r < nrows;
              r++, y += heightOnComponent + vgap) {
            int i = r * ncols + c;
            if (i < ncomponents) {
              parent.getComponent(i).setBounds(x, y, widthOnComponent, heightOnComponent);
            }
          }
        }
      } else {
        for (int c = 0, x = (parent.width - insets.right - widthOnComponent) - extraWidthAvailable;
            c < ncols;
            c++, x -= widthOnComponent + hgap) {
          for (int r = 0, y = insets.top + extraHeightAvailable;
              r < nrows;
              r++, y += heightOnComponent + vgap) {
            int i = r * ncols + c;
            if (i < ncomponents) {
              parent.getComponent(i).setBounds(x, y, widthOnComponent, heightOnComponent);
            }
          }
        }
      }
    }
  }
Exemplo n.º 3
0
  /**
   * Lays out the specified container using this layout.
   *
   * <p>This method reshapes the components in the specified target container in order to satisfy
   * the constraints of the <code>GridLayout</code> object.
   *
   * <p>The grid layout manager determines the size of individual components by dividing the free
   * space in the container into equal-sized portions according to the number of rows and columns in
   * the layout. The container's free space equals the container's size minus any insets and any
   * specified horizontal or vertical gap. All components in a grid layout are given the same size.
   *
   * @param parent the container in which to do the layout
   * @see java.awt.Container
   * @see java.awt.Container#doLayout
   */
  public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
      Insets insets = parent.getInsets();
      int ncomponents = parent.getComponentCount();
      int nrows = rows;
      int ncols = cols;
      boolean ltr = parent.getComponentOrientation().isLeftToRight();

      if (ncomponents == 0) {
        return;
      }
      if (nrows > 0) {
        ncols = (ncomponents + nrows - 1) / nrows;
      } else {
        nrows = (ncomponents + ncols - 1) / ncols;
      }
      int w = parent.width - (insets.left + insets.right);
      int h = parent.height - (insets.top + insets.bottom);
      w = (w - (ncols - 1) * hgap) / ncols;
      h = (h - (nrows - 1) * vgap) / nrows;

      if (ltr) {
        for (int c = 0, x = insets.left; c < ncols; c++, x += w + hgap) {
          for (int r = 0, y = insets.top; r < nrows; r++, y += h + vgap) {
            int i = r * ncols + c;
            if (i < ncomponents) {
              parent.getComponent(i).setBounds(x, y, w, h);
            }
          }
        }
      } else {
        for (int c = 0, x = parent.width - insets.right - w; c < ncols; c++, x -= w + hgap) {
          for (int r = 0, y = insets.top; r < nrows; r++, y += h + vgap) {
            int i = r * ncols + c;
            if (i < ncomponents) {
              parent.getComponent(i).setBounds(x, y, w, h);
            }
          }
        }
      }
    }
  }