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

voidequalizeSize(final JComponent... components)
Resizes the given components making them equal in size.
if (components == null || components.length == 0)
    return;
final Dimension prefSize = components[0].getPreferredSize();
final Dimension maxSize = components[0].getMaximumSize();
for (JComponent c : components) {
    ensureSize(prefSize, c.getPreferredSize());
    ensureSize(maxSize, c.getMaximumSize());
for (JComponent c : components) {
    c.setPreferredSize(prefSize);
    c.setMaximumSize(maxSize);
voidequalizeSizes(JComponent[] components)
Sets the items in aComponents to the same size.
Dimension targetSize = new Dimension(0, 0);
for (int i = 0; i < components.length; i++) {
    JComponent comp = components[i];
    Dimension compSize = comp.getPreferredSize();
    double width = Math.max(targetSize.getWidth(), compSize.getWidth());
    double height = Math.max(targetSize.getHeight(), compSize.getHeight());
    targetSize.setSize(width, height);
setSizes(components, targetSize);
voidevenSizes(JComponent[] components)
Makes all sizes (min, pref, max) the same to the respective maximum of all given components.
Dimension minSize = new Dimension(Integer.MIN_VALUE, Integer.MIN_VALUE);
Dimension prefSize = new Dimension(Integer.MIN_VALUE, Integer.MIN_VALUE);
Dimension maxSize = new Dimension(Integer.MIN_VALUE, Integer.MIN_VALUE);
for (int i = 0; i < components.length; i++) {
    Dimension min = components[i].getMinimumSize();
    if (min.width > minSize.width)
        minSize.width = min.width;
    if (min.height > minSize.height)
...
RectanglefindCenterBounds(Dimension componentSize)
Helps client code place components on the center of the screen.
return findCenterBounds(getCurrentGraphicsConfiguration(), componentSize);
voidfixSize(JComponent c, Dimension d)
fix Size
c.setMaximumSize(d);
c.setPreferredSize(d);
c.setMaximumSize(d);
c.revalidate();
voidfixSize(JComponent... items)
fix Size
for (JComponent item : items) {
    item.setMinimumSize(item.getPreferredSize());
    item.setMaximumSize(item.getPreferredSize());
    item.setSize(item.getPreferredSize());
TfixSize(T comp, int width)
Fixes the size of a JComponent to the given width

If a value of -1 is passed along, the preferred size is used instead.

return fixSize(comp, width, -1);
voidforceSize(JComponent component, int width, int height)
force Size
Dimension d = new Dimension(width, height);
component.setMinimumSize(d);
component.setMaximumSize(d);
component.setPreferredSize(d);
JComponentfreezeSize(JComponent c, Dimension s)
freeze Size
c.validate();
c.setMinimumSize(s);
c.setMaximumSize(s);
c.setPreferredSize(s);
return c;
RectanglegetCentralLocation(Component sourceComp, Component comp, Dimension size)
get Central Location
final Rectangle position;
if (sourceComp != null) {
    Point compLocation = new Point(0, 0);
    SwingUtilities.convertPointToScreen(compLocation, sourceComp);
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] devices = e.getScreenDevices();
    Rectangle displayBounds = null;
    for (GraphicsDevice device : devices) {
...