Java JComponent Size fixSize(T comp, int width)

Here you can find the source of fixSize(T comp, int width)

Description

Fixes the size of a JComponent to the given width

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

License

Apache License

Parameter

Parameter Description
comp the component to fix the size of
width the fixed width

Return

the updated component

Declaration

public static <T extends JComponent> T fixSize(T comp, int width) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2011 Danish Maritime Authority.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*from   w ww  .  j a  v a2  s .  co  m*/
 */

import java.awt.Dimension;

import javax.swing.JComponent;

public class Main {
    /**
     * Fixes the size of a {@linkplain JComponent} to the given width and height.
     * <p>
     * If a value of -1 is passed along, the preferred size is used instead.
     * 
     * @param comp the component to fix the size of
     * @param width the fixed width
     * @param height the fixed height
     * @return the updated component
     */
    public static <T extends JComponent> T fixSize(T comp, int width, int height) {
        // Sanity check
        if (comp == null) {
            return null;
        }

        if (width == -1) {
            width = (int) comp.getPreferredSize().getWidth();
        }
        if (height == -1) {
            height = (int) comp.getPreferredSize().getHeight();
        }
        Dimension dim = new Dimension(width, height);
        comp.setPreferredSize(dim);
        comp.setMaximumSize(dim);
        comp.setMinimumSize(dim);
        comp.setSize(dim);
        return comp;
    }

    /**
     * Fixes the size of a {@linkplain JComponent} to the given width
     * <p>
     * If a value of -1 is passed along, the preferred size is used instead.
     * 
     * @param comp the component to fix the size of
     * @param width the fixed width
     * @return the updated component
     */
    public static <T extends JComponent> T fixSize(T comp, int width) {
        return fixSize(comp, width, -1);
    }
}

Related

  1. equalizeSizes(JComponent[] components)
  2. evenSizes(JComponent[] components)
  3. findCenterBounds(Dimension componentSize)
  4. fixSize(JComponent c, Dimension d)
  5. fixSize(JComponent... items)
  6. forceSize(JComponent component, int width, int height)
  7. freezeSize(JComponent c, Dimension s)
  8. getCentralLocation(Component sourceComp, Component comp, Dimension size)
  9. getComponentOfSameSize(final JComponent c)