Java JButton Settings getPreferredButtonSize(AbstractButton b, int textIconGap)

Here you can find the source of getPreferredButtonSize(AbstractButton b, int textIconGap)

Description

Determines the preferred width and height of an AbstractButton, given the gap between the button’s text and icon.

License

Open Source License

Parameter

Parameter Description
b the button whose preferred size is determined.
textIconGap the gap between the button’s text and icon.

Return

a Dimension object whose width and height fields indicate the preferred extent in pixels.

Declaration

public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap) 

Method Source Code

//package com.java2s;
/* BasicGraphicsUtils.java
   Copyright (C) 2003 Free Software Foundation, Inc.
    //from  www  .  j  a  va2s. c  o  m
This file is part of GNU Classpath.
    
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
    
GNU Classpath 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
General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
    
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
    
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

import java.awt.Dimension;

import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.AbstractButton;
import javax.swing.SwingUtilities;

public class Main {
    /**
     * Determines the preferred width and height of an AbstractButton,
     * given the gap between the button’s text and icon.
     *
     * @param b the button whose preferred size is determined.
     *
     * @param textIconGap the gap between the button’s text and
     *        icon.
     *
     * @return a <code>Dimension</code> object whose <code>width</code>
     *         and <code>height</code> fields indicate the preferred
     *         extent in pixels.
     *
     * @see javax.swing.SwingUtilities#layoutCompoundLabel
     */
    public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap) {
        Rectangle contentRect;
        Rectangle viewRect;
        Rectangle iconRect = new Rectangle();
        Rectangle textRect = new Rectangle();
        Insets insets = b.getInsets();
        Insets margin = b.getMargin();

        /* For determining the ideal size, do not assume a size restriction. */
        viewRect = new Rectangle(0, 0, /* width */ Integer.MAX_VALUE, /* height */ Integer.MAX_VALUE);

        /* java.awt.Toolkit.getFontMetrics is deprecated. However, it
        * seems not obvious how to get to the correct FontMetrics object
        * otherwise. The real problem probably is that the method
        * javax.swing.SwingUtilities.layoutCompundLabel should take a
        * LineMetrics, not a FontMetrics argument. But fixing this that
        * would change the public API.
        */
        SwingUtilities.layoutCompoundLabel(b, // for the component orientation
                b.getFontMetrics(b.getFont()), // see comment above
                b.getText(), b.getIcon(), SwingUtilities.TOP, // important:
                SwingUtilities.LEFT, // large vrect, stick to the top left
                b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect,
                textIconGap);

        /*  +------------------------+       +------------------------+
         *  |                        |       |                        |
         *  | ICON                   |       | CONTENTCONTENTCONTENT  |
         *  |          TEXTTEXTTEXT  |  -->  | CONTENTCONTENTCONTENT  |
         *  |          TEXTTEXTTEXT  |       | CONTENTCONTENTCONTENT  |
         *  +------------------------+       +------------------------+
         */
        contentRect = textRect.union(iconRect);

        return new Dimension(insets.left + margin.left + contentRect.width + insets.right + margin.right,
                insets.top + margin.top + contentRect.height + insets.bottom + margin.bottom);
    }
}

Related

  1. getDefaultFatButtonMargin()
  2. getHudControlShadowSize(AbstractButton button)
  3. getIcon(AbstractButton b)
  4. getListenedButtonsFor(Container c)
  5. getMonospaceButton(final String text, final int size)
  6. getPreferredButtonSize(AbstractButton b, int textIconGap)
  7. getPressedButtonIcon(int x, int y)
  8. getRadioButtonBorder(int fontSize, boolean ltr)
  9. getSelectedButton(ButtonGroup group)