Java JComponent Width setPreferredWidth(final JComponent component, final int width)

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

Description

Sets the width of a JComponent without changing its height.

License

Apache License

Parameter

Parameter Description
component the component
width the width

Declaration

public static void setPreferredWidth(final JComponent component, final int width) 

Method Source Code

//package com.java2s;
/**************************************************************************
 * alpha-Flow: distributed case files in form of active documents
 * (supporting knowledge-driven ad-hoc processes in healthcare)
 * ==============================================
 * Copyright (C) 2009-2012 by //w  ww .  jav a  2s  .c om
 *   - Christoph P. Neumann (http://www.chr15t0ph.de)
 **************************************************************************
 * 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.
 **************************************************************************
 * $Id: Swing_Helper.java 3583 2012-02-16 01:52:45Z cpn $
 *************************************************************************/

import java.awt.Dimension;

import javax.swing.JComponent;

public class Main {
    /**
     * Sets the width of a JComponent without changing its height.
     * 
     * @param component
     *            the component
     * @param width
     *            the width
     */
    public static void setPreferredWidth(final JComponent component, final int width) {
        final int height = (int) component.getPreferredSize().getHeight();
        component.setPreferredSize(new Dimension(width, height));
    }
}

Related

  1. setMaximumWidth(final JComponent component, final int width)
  2. setMaximumWidth(JComponent component, int width)
  3. setMaxWidth(JComponent comp, int width)
  4. setMinimumWidth(JComponent component, int minWidth)
  5. setMinimumWidth(JComponent component, int width)
  6. setPreferredWidth(final JComponent component, final int width)
  7. setPreferredWidth(int width, JComponent... components)
  8. setPreferredWidth(JComponent comp, int iWidth)
  9. setPreferredWidth(JComponent comp, int width)