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

voidsetAllSizes(JComponent component, int width, int height)
set the component's minimum, maximum and preferred sizes
Dimension dim = new Dimension(width, height);
component.setMinimumSize(dim);
component.setMaximumSize(dim);
component.setPreferredSize(dim);
voidsetComponentSize(int width, int height, JComponent l)
set Component Size
l.setPreferredSize(new Dimension(width, height));
l.setMinimumSize(new Dimension(width, height));
if (l instanceof JTextField || l instanceof JComboBox) {
    l.setMaximumSize(new Dimension(Short.MAX_VALUE, height));
voidsetComponentSize(JComponent comp, int defWidth, int defHeight, Preferences prefs, String name)
Set the preferred size of a JComponent by restoring from package Preferences object.
if (defWidth == 0 || defHeight == 0) {
    String width = prefs.get(name + "_width", null);
    if (width == null) {
        return;
int width = prefs.getInt(name + "_width", defWidth);
int height = prefs.getInt(name + "_height", defHeight);
...
voidsetComponentSize(JComponent comp, int width, int height)
set Component Size
comp.setPreferredSize(new Dimension(width, height));
comp.setMinimumSize(comp.getPreferredSize());
comp.setMaximumSize(comp.getPreferredSize());
voidsetFixedSize(JComponent component, Dimension size)
set Fixed Size
component.setPreferredSize(size);
component.setMinimumSize(size);
component.setMaximumSize(size);
voidsetFixedSize(JComponent component, Dimension size)
set Fixed Size
component.setPreferredSize(size);
component.setMinimumSize(size);
component.setMaximumSize(size);
voidsetFixedSize(JComponent component, int width, int height)

Set the fixed size to given component.

Dimension size = new Dimension(width, height);
component.setMaximumSize(size);
component.setMinimumSize(size);
component.setPreferredSize(size);
voidsetMaximumSize(Dimension d, JComponent... components)
Set the maximum size of the given components.
for (JComponent c : components) {
    c.setMaximumSize(d);
voidsetMaximumSize(Dimension maxComponentSize, JComponent component)
set Maximum Size
final Dimension componentSize = component.getPreferredSize();
maxComponentSize.width = Math.max(maxComponentSize.width, (int) componentSize.getWidth());
maxComponentSize.height = Math.max(maxComponentSize.height, (int) componentSize.getHeight());
voidsetMaxPreferredSize(JComponent comp)
set Max Preferred Size
comp.setPreferredSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));