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

The following examples show how to use java.awt.Component#getMinimumSize() . 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 minimumLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
        Dimension dim = new Dimension(0, 0);
        int componentCount = parent.getComponentCount();

        for (int i = 0; i < componentCount; i++) {
            Component c = parent.getComponent(i);
            if (c.isVisible()) {
                Dimension d = c.getMinimumSize();
                dim.height = Math.max(dim.height, d.height);
                if (i > 0) {
                    dim.width += hgap;
                }
                dim.width += d.width;
            }
        }
        Insets insets = parent.getInsets();
        dim.width += insets.left + insets.right + 2 * hgap;
        dim.height += insets.top + insets.bottom + 2 * vgap;
        return dim;
    }
}
 
Example 2
public void test(final Component component) {
    final Dimension psize = component.getPreferredSize();
    psize.width += 200;
    if (Objects.equals(psize, component.getPreferredSize())) {
        throw new RuntimeException("PreferredSize is wrong");
    }
    final Dimension msize = component.getMaximumSize();
    msize.width += 200;
    if (Objects.equals(msize, component.getMaximumSize())) {
        throw new RuntimeException("MaximumSize is wrong");
    }
    final Dimension misize = component.getMinimumSize();
    misize.width += 200;
    if (Objects.equals(misize, component.getMinimumSize())) {
        throw new RuntimeException("MinimumSize is wrong");
    }
}
 
Example 3
Source Project: Spark   File: VerticalFlowLayout.java    License: Apache License 2.0 6 votes vote down vote up
/**
    * Returns the minimum size needed to layout the target container.
    *
    * @param target the component to lay out.
    * @return the minimum layout dimension.
    */
   @Override
public Dimension minimumLayoutSize(Container target) {
       Dimension tarsiz = new Dimension(0, 0);

       for (int i = 0; i < target.getComponentCount(); i++) {
           Component m = target.getComponent(i);
           if (m.isVisible()) {
               Dimension d = m.getMinimumSize();
               tarsiz.width = Math.max(tarsiz.width, d.width);
               if (i > 0) {
                   tarsiz.height += vgap;
               }
               tarsiz.height += d.height;
           }
       }
       Insets insets = target.getInsets();
       tarsiz.width += insets.left + insets.right + hgap * 2;
       tarsiz.height += insets.top + insets.bottom + vgap * 2;
       return tarsiz;
   }
 
Example 4
public void test(final Component component) {
    final Dimension psize = component.getPreferredSize();
    psize.width += 200;
    if (Objects.equals(psize, component.getPreferredSize())) {
        throw new RuntimeException("PreferredSize is wrong");
    }
    final Dimension msize = component.getMaximumSize();
    msize.width += 200;
    if (Objects.equals(msize, component.getMaximumSize())) {
        throw new RuntimeException("MaximumSize is wrong");
    }
    final Dimension misize = component.getMinimumSize();
    misize.width += 200;
    if (Objects.equals(misize, component.getMinimumSize())) {
        throw new RuntimeException("MinimumSize is wrong");
    }
}
 
Example 5
public Dimension getSize(Container parent, boolean minimum) {
    int n = parent.getComponentCount();
    Insets insets = parent.getInsets();
    Dimension d = new Dimension();
    for (int i = 0; i < n; i++) {
        Component comp = parent.getComponent(i);
        if (comp instanceof EnableButton) {
            continue;
        }
        Dimension p = (minimum
                       ? comp.getMinimumSize()
                       : comp.getPreferredSize());
        if (horizontal) {
            d.width += p.width;
            if (d.height < p.height) {
                d.height = p.height;
            }
        } else {
            if (d.width < p.width) {
                d.width = p.width;
            }
            d.height += p.height;
        }
    }
    d.width += (insets.left + insets.right);
    d.height += (insets.top + insets.bottom);
    return d;
}
 
Example 6
Source Project: pumpernickel   File: BoxTabbedPaneUI.java    License: MIT License 5 votes vote down vote up
private Dimension getLayoutSize(Container parent, boolean preferred) {
	int tabPlacement = ((JTabbedPane) parent).getTabPlacement();
	boolean verticalPlacement = tabPlacement == JTabbedPane.TOP
			|| tabPlacement == JTabbedPane.BOTTOM;
	Dimension additional = new Dimension(0, 0);
	Dimension max = new Dimension(0, 0);
	for (int a = 0; a < parent.getComponentCount(); a++) {
		Component c = parent.getComponent(a);
		Dimension d = preferred ? c.getPreferredSize() : c
				.getMinimumSize();
		if (c instanceof UIResourcePanel) {
			if (verticalPlacement) {
				additional.height += d.height;
				additional.width = Math.max(additional.width, d.width);
			} else {
				additional.width += d.width;
				additional.height = Math.max(additional.height,
						d.height);
			}
		} else {
			max.width = Math.max(max.width, d.width);
			max.height = Math.max(max.height, d.height);
		}
	}
	if (verticalPlacement) {
		return new Dimension(Math.max(additional.width, max.width),
				additional.height + max.height);
	}
	return new Dimension(additional.width + max.width, Math.max(
			additional.height, max.height));
}
 
Example 7
public Dimension getSize(Container parent, boolean minimum) {
    int n = parent.getComponentCount();
    Insets insets = parent.getInsets();
    Dimension d = new Dimension();
    for (int i = 0; i < n; i++) {
        Component comp = parent.getComponent(i);
        if (comp instanceof EnableButton) {
            continue;
        }
        Dimension p = (minimum
                       ? comp.getMinimumSize()
                       : comp.getPreferredSize());
        if (horizontal) {
            d.width += p.width;
            if (d.height < p.height) {
                d.height = p.height;
            }
        } else {
            if (d.width < p.width) {
                d.width = p.width;
            }
            d.height += p.height;
        }
    }
    d.width += (insets.left + insets.right);
    d.height += (insets.top + insets.bottom);
    return d;
}
 
Example 8
public Dimension getSize(Container parent, boolean minimum) {
    int n = parent.getComponentCount();
    Insets insets = parent.getInsets();
    Dimension d = new Dimension();
    for (int i = 0; i < n; i++) {
        Component comp = parent.getComponent(i);
        if (comp instanceof EnableButton) {
            continue;
        }
        Dimension p = (minimum
                       ? comp.getMinimumSize()
                       : comp.getPreferredSize());
        if (horizontal) {
            d.width += p.width;
            if (d.height < p.height) {
                d.height = p.height;
            }
        } else {
            if (d.width < p.width) {
                d.width = p.width;
            }
            d.height += p.height;
        }
    }
    d.width += (insets.left + insets.right);
    d.height += (insets.top + insets.bottom);
    return d;
}
 
Example 9
@Override
public Dimension getPreferredSize() {
    Component[] childComponents = getComponents();
    if (childComponents.length == 0) {
        return minSize;
    } else {
        // TODO: should be computed just when child components are added/removed
        int minWidth = 0;
        for (Component component : childComponents) {
            int minChildComponentWidth = component.getMinimumSize().width;
            if (minWidth < minChildComponentWidth) minWidth = minChildComponentWidth;
        }
        return new Dimension(minWidth, imageSize.height);
    }
}
 
Example 10
Source Project: netbeans   File: SnapshotsWindowUI.java    License: Apache License 2.0 5 votes vote down vote up
public Dimension minimumLayoutSize(Container parent) {
    int minw = 0;
    int minh = 0;
    for (Component c : parent.getComponents()) {
        Dimension min = c.getMinimumSize();
        minw += min.width;
        minh = Math.max(minh, min.height);
    }
    minw += HGAP * (parent.getComponentCount() - 1);
    return new Dimension(minw, minh);
}
 
Example 11
Source Project: netbeans   File: VerticalFlowLayout.java    License: Apache License 2.0 5 votes vote down vote up
private Dimension computeDimension(Container parent, int type) {
    Insets insets = parent.getInsets();
    int x = insets.left;
    int y = insets.top;
    int columnWidth = 0;
    // int limitHeight = parent.getHeight() - insets.bottom;
    int maxY = 0;

    for (Component c : this.components) {
        if (c.isVisible()) {
            Dimension d;

            switch (type) {
                case 0:
                    d = c.getPreferredSize();
                    break;
                case 1:
                    d = c.getMinimumSize();
                    break;
                default:
                    d = c.getMaximumSize();
                    break;
            }
            columnWidth = Math.max(columnWidth, d.width);
            /*
            if (limitHeight != 0 && y + d.height >= limitHeight) {
                x += columnWidth + this.hgap;
                y = insets.top;
                columnWidth = d.width;
            }
            */
            y += d.height;
            maxY = Math.max(y, maxY);
            y += this.vgap;
        }
    }
    x += columnWidth;
    return new Dimension(x, maxY);
}
 
Example 12
public float getMinHeight (Component widget) {
	return widget.getMinimumSize().height;
}
 
Example 13
public float getMinHeight (Component widget) {
	return widget.getMinimumSize().height;
}
 
Example 14
public float getMinWidth (Component widget) {
	return widget.getMinimumSize().width;
}
 
Example 15
/**
 * Returns the minimum or preferred dimension needed to layout the target
 * container.
 *
 * @param target target to get layout size for
 * @param preferred should preferred size be calculated
 * @return the dimension to layout the target container
 */
private Dimension layoutSize(final Container target, final boolean preferred) {
	synchronized (target.getTreeLock()) {
		//  Each row must fit with the width allocated to the containter.
		//  When the container width = 0, the preferred width of the container
		//  has not yet been calculated so lets ask for the maximum.

		int targetWidth = target.getSize().width;

		if (targetWidth == 0) {
			targetWidth = Integer.MAX_VALUE;
		}

		final int hgap = getHgap();
		final int vgap = getVgap();
		final Insets insets = target.getInsets();
		final int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
		final int maxWidth = targetWidth - horizontalInsetsAndGap;

		//  Fit components into the allowed width

		final Dimension dim = new Dimension(0, 0);
		int rowWidth = 0;
		int rowHeight = 0;

		final int nmembers = target.getComponentCount();

		for (int i = 0; i < nmembers; i++) {
			final Component m = target.getComponent(i);

			if (m.isVisible()) {
				final Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();

				//  Can't add the component to current row. Start a new row.

				if (rowWidth + d.width > maxWidth) {
					addRow(dim, rowWidth, rowHeight);
					rowWidth = 0;
					rowHeight = 0;
				}

				//  Add a horizontal gap for all components after the first

				if (rowWidth != 0) {
					rowWidth += hgap;
				}

				rowWidth += d.width;
				rowHeight = Math.max(rowHeight, d.height);
			}
		}

		addRow(dim, rowWidth, rowHeight);

		dim.width += horizontalInsetsAndGap;
		dim.height += insets.top + insets.bottom + vgap * 2;

		//	When using a scroll pane or the DecoratedLookAndFeel we need to
		//  make sure the preferred size is less than the size of the
		//  target containter so shrinking the container size works
		//  correctly. Removing the horizontal gap is an easy way to do this.

		final Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);

		if (scrollPane != null && target.isValid()) {
			dim.width -= (hgap + 1);
		}

		return dim;
	}
}
 
Example 16
/**
 * Returns the minimum or preferred dimension needed to layout the target
 * container.
 * 
 * @param target
 *            target to get layout size for
 * @param preferred
 *            should preferred size be calculated
 * @return the dimension to layout the target container
 */
private Dimension layoutSize(Container target, boolean preferred)
{
	synchronized (target.getTreeLock())
	{
		// Each row must fit with the width allocated to the containter.
		// When the container width = 0, the preferred width of the
		// container
		// has not yet been calculated so lets ask for the maximum.

		int targetWidth = target.getSize().width;

		if (targetWidth == 0)
			targetWidth = Integer.MAX_VALUE;

		final int hgap = getHgap();
		final int vgap = getVgap();
		final Insets insets = target.getInsets();
		final int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
		final int maxWidth = targetWidth - horizontalInsetsAndGap;

		// Fit components into the allowed width

		final Dimension dim = new Dimension(0, 0);
		int rowWidth = 0;
		int rowHeight = 0;

		final int nmembers = target.getComponentCount();

		for (int i = 0; i < nmembers; i++)
		{
			final Component m = target.getComponent(i);

			if (m.isVisible())
			{
				final Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();

				// Can't add the component to current row. Start a new row.

				if (rowWidth + d.width > maxWidth)
				{
					addRow(dim, rowWidth, rowHeight);
					rowWidth = 0;
					rowHeight = 0;
				}

				// Add a horizontal gap for all components after the first

				if (rowWidth != 0)
				{
					rowWidth += hgap;
				}

				rowWidth += d.width;
				rowHeight = Math.max(rowHeight, d.height);
			}
		}

		addRow(dim, rowWidth, rowHeight);

		dim.width += horizontalInsetsAndGap;
		dim.height += insets.top + insets.bottom + vgap * 2;

		// When using a scroll pane or the DecoratedLookAndFeel we need to
		// make sure the preferred size is less than the size of the
		// target containter so shrinking the container size works
		// correctly. Removing the horizontal gap is an easy way to do this.

		final Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);

		if (scrollPane != null && target.isValid())
		{
			dim.width -= (hgap + 1);
		}

		return dim;
	}
}
 
Example 17
/**
 * Returns the minimum or preferred dimension needed to layout the target
 * container.
 *
 * @param target    target to get layout size for
 * @param preferred should preferred size be calculated
 * @return the dimension to layout the target container
 */
private Dimension layoutSize(final Container target, final boolean preferred) {
    synchronized (target.getTreeLock()) {
        // Each row must fit with the width allocated to the containter.
        // When the container width = 0, the preferred width of the
        // container
        // has not yet been calculated so lets ask for the maximum.

        int targetWidth = target.getSize().width;

        if (targetWidth == 0) {
            targetWidth = Integer.MAX_VALUE;
        }

        int hgap = getHgap();
        int vgap = getVgap();
        Insets insets = target.getInsets();
        int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
        int maxWidth = targetWidth - horizontalInsetsAndGap;

        // Fit components into the allowed width

        Dimension dim = new Dimension(0, 0);
        int rowWidth = 0;
        int rowHeight = 0;

        int nmembers = target.getComponentCount();

        for (int i = 0; i < nmembers; i++) {
            Component m = target.getComponent(i);

            if (m.isVisible()) {
                Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();

                // Can't add the component to current row. Start a new row.

                if (rowWidth + d.width > maxWidth) {
                    addRow(dim, rowWidth, rowHeight);
                    rowWidth = 0;
                    rowHeight = 0;
                }

                // Add a horizontal gap for all components after the first

                if (rowWidth != 0) {
                    rowWidth += hgap;
                }

                rowWidth += d.width;
                rowHeight = Math.max(rowHeight, d.height);
            }
        }

        addRow(dim, rowWidth, rowHeight);

        dim.width += horizontalInsetsAndGap;
        dim.height += insets.top + insets.bottom + vgap * 2;

        // When using a scroll pane or the DecoratedLookAndFeel we need to
        // make sure the preferred size is less than the size of the
        // target containter so shrinking the container size works
        // correctly. Removing the horizontal gap is an easy way to do this.

        Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);

        if (scrollPane != null) {
            dim.width -= (hgap + 1);
        }

        return dim;
    }
}
 
Example 18
public float getMinWidth (Component widget) {
	return widget.getMinimumSize().width;
}
 
Example 19
Source Project: pumpernickel   File: RowLayout.java    License: MIT License 2 votes vote down vote up
/**
 * Returns the minimum size of a component. Subclasses are encouraged to
 * override this.
 * 
 * @param c
 *            the component to retrieve the minimum size.
 * @return the minimum size of the argument.
 */
protected Dimension getMinimumSize(Component c) {
	return c.getMinimumSize();
}
 
Example 20
public Dimension minimumLayoutSize(Container parent) {
        synchronized (parent.getTreeLock()) {
            int width = 0;
            int height = 0;

            for (Component comp: components) {
                Dimension size = comp.getMinimumSize();
                width = Math.max(size.width, width);
                height = Math.max(size.height, height);
            }

            Insets insets = parent.getInsets();
            width += insets.left + insets.right;
            height += insets.top + insets.bottom;

            return new Dimension(width, height);
        }
    }