Java Utililty Methods JComponent Size

List of utility methods to do JComponent Size

Description

The list of methods to do JComponent Size are organized into topic(s).

Method

voidmakeSameSize(JComponent... comps)
Makes all Compontens the same Size
if (comps.length == 0) {
    return;
int max = 0;
for (JComponent comp1 : comps) {
    int w = comp1.getPreferredSize().width;
    max = w > max ? w : max;
Dimension dim = new Dimension(max, comps[0].getPreferredSize().height);
for (JComponent comp : comps) {
    comp.setPreferredSize(dim);
voidmakeSameSize(JComponent[] comps)
make Same Size
if (comps.length == 0) {
    return;
int max = 0;
for (int i = 0; i < comps.length; i++) {
    int w = comps[i].getPreferredSize().width;
    max = w > max ? w : max;
Dimension dim = new Dimension(max, comps[0].getPreferredSize().height);
for (int i = 0; i < comps.length; i++) {
    comps[i].setPreferredSize(dim);
TminSize(T comp, int width)
Sets the minimum size of the JComponent
if (comp == null) {
    return null;
comp.setMinimumSize(new Dimension(width, comp.getMinimumSize().height));
return comp;
BufferedImagepaint(final Component c, Dimension size)
Paint a Component as a BufferedImage using its preferred size.
if (size == null)
    size = c.getPreferredSize();
final BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
try {
    Runnable runnable1 = new Runnable() {
        private void revalidate(Component c) {
            if (c instanceof Container) {
                Container c2 = (Container) c;
...
DimensionpreferredSize(JComponent component)
preferred Size
Dimension size = component.getPreferredSize();
component.setSize(size);
return size;
voidresize(JComponent component, Dimension size)
resize
component.setPreferredSize(size);
component.setMaximumSize(size);
component.setMinimumSize(size);
voidresizeComponentWidth(JComponent currentComponent, double maxComponentWidth)
resize Component Width
currentComponent.setPreferredSize(
        new Dimension((int) maxComponentWidth, (int) currentComponent.getPreferredSize().getHeight()));
currentComponent.setMaximumSize(
        new Dimension((int) maxComponentWidth, (int) currentComponent.getMaximumSize().getHeight()));
currentComponent.setMinimumSize(
        new Dimension((int) maxComponentWidth, (int) currentComponent.getMinimumSize().getHeight()));
voidrestrictWindowMinimumSize(final Window wnd, final Dimension minSize)
Adds listener to window that ensures that the window is not resized less then specified size.
restrictMinimumSizeImpl(wnd, minSize);
DimensionsameSize(JComponent[] components)
same Size
int width = 0;
int height = 0;
for (int i = 0; i < components.length; i++) {
    Dimension size = components[i].getPreferredSize();
    width = Math.max(width, size.width);
    height = Math.max(height, size.height);
Dimension result = new Dimension(width, height);
...
voidsaveComponentSize(JComponent comp, Preferences prefs, String name)
Save the size of a JComponent to a package Preferences object.
Dimension dims = null;
if (comp instanceof JViewport) {
    dims = ((JViewport) comp).getExtentSize();
} else {
    dims = comp.getSize();
prefs.putInt(name + "_width", dims.width);
prefs.putInt(name + "_height", dims.height);
...