Java JComponent Width setPreferredWidth(JComponent comp, int width)

Here you can find the source of setPreferredWidth(JComponent comp, int width)

Description

Set the preferred width on a component

License

Open Source License

Parameter

Parameter Description
comp component
width width

Declaration

public static void setPreferredWidth(JComponent comp, int width) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  . j  ava2s  .  c  o  m
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.Dimension;

import javax.swing.JComponent;

public class Main {
    /**
     * Set the preferred width on a component
     *
     * @param comp  component
     * @param width width
     */
    public static void setPreferredWidth(JComponent comp, int width) {
        int height = comp.getPreferredSize().height;
        if (!checkHeight(height)) {
            //For now just set the height to some reasonable amount and use that
            height = 24;
        }
        comp.setPreferredSize(new Dimension(width, height));
    }

    /**
     * Check the height against a value
     *
     * @param height  the value to check
     *
     * @return return height > 100;
     */
    public static boolean checkHeight(int height) {
        if (height > 100) {
            //            LogUtil.printMessage("Got large height when setting preferred size:" + height);
            //            LogUtil.printMessage(LogUtil.getStackTrace());
            return false;
        }
        return true;
    }
}

Related

  1. setPreferredWidth(final JComponent component, final int width)
  2. setPreferredWidth(final JComponent component, final int width)
  3. setPreferredWidth(int width, JComponent... components)
  4. setPreferredWidth(JComponent comp, int iWidth)
  5. setPreferredWidth(JComponent comp, int width)
  6. setPreferredWidth(JComponent component, int width)
  7. setPreferredWidth(JComponent component, int width)
  8. setPreferredWidth(JComponent component, int width)
  9. setPrefferedWidth(JComponent c, int w)