Java JLabel Size getPreferredSize(JLabel label)

Here you can find the source of getPreferredSize(JLabel label)

Description

get Preferred Size

License

Open Source License

Declaration

public static Dimension getPreferredSize(JLabel label) 

Method Source Code

//package com.java2s;
/* /*ww w  . j ava 2 s. co  m*/
 * The MIT License
 *
 * Copyright 2014 Kamnev Georgiy (nt.gocha@gmail.com).
 */

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Rectangle2D;
import java.lang.ref.WeakReference;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

public class Main {
    private static WeakReference<Component> host = null;

    public static Dimension getPreferredSize(JLabel label) {
        if (label == null) {
            throw new IllegalArgumentException("label==null");
        }

        Dimension preferredSize = null;

        Dimension dm = getTextDimension(label);
        dm = extendByIcon(label, dm);
        dm = extendByBorder(label, dm);
        preferredSize = dm;

        return preferredSize;
    }

    public static Dimension getTextDimension(JLabel label) {
        if (label == null) {
            throw new IllegalArgumentException("label==null");
        }

        int width = 0;
        int height = 0;

        String text = label.getText();
        if (text != null) {
            Component c = getHost();
            Graphics2D gs = null;

            if (c == null) {
                gs = (Graphics2D) label.getGraphics();
            } else {
                gs = (Graphics2D) c.getGraphics();
            }

            FontMetrics fm = label.getFontMetrics(label.getFont());

            if (gs == null) {
                height = fm.getHeight();
                width = fm.stringWidth(text);
            } else {
                Rectangle2D rect = fm.getStringBounds(text, gs);
                double w = rect.getWidth();
                double h = rect.getHeight();
                width = (int) w;
                height = (int) h;
                gs.dispose();
            }
        }

        return new Dimension(width, height);
    }

    public static Dimension extendByIcon(JLabel label, Dimension dm) {
        if (label == null) {
            throw new IllegalArgumentException("label==null");
        }
        if (dm == null) {
            throw new IllegalArgumentException("dm==null");
        }

        javax.swing.Icon icon = label.getIcon();
        if (icon == null)
            return dm;

        int vertTextPos = label.getVerticalTextPosition();
        int horzTextPos = label.getHorizontalTextPosition();

        int iconTextGap = label.getIconTextGap();
        int icoWidth = icon.getIconWidth();
        int icoHeight = icon.getIconHeight();

        if (vertTextPos == SwingConstants.CENTER) {
            if (horzTextPos == SwingConstants.CENTER) {
                int nw = -1;
                int nh = -1;
                if (dm.getWidth() < icoWidth)
                    nw = icoWidth;
                if (dm.getHeight() < icoHeight)
                    nh = icoHeight;
                if (nw < 0 && nh < 0)
                    return dm;
                return new Dimension(nw < 0 ? dm.width : nw,
                        nh < 0 ? dm.height : nh);
            } else {
                int nh = -1;
                if (dm.getHeight() < icoHeight)
                    nh = icoHeight;
                return new Dimension(icoWidth + iconTextGap + dm.width,
                        nh < 0 ? dm.height : nh);
            }
        } else {
            int nw = -1;
            if (dm.getWidth() < icoWidth)
                nw = icoWidth;
            return new Dimension(nw < 0 ? dm.width : nw, dm.height
                    + iconTextGap + icoHeight);
        }
    }

    public static Dimension extendByBorder(JLabel label, Dimension dm) {
        if (label == null) {
            throw new IllegalArgumentException("label==null");
        }
        if (dm == null) {
            throw new IllegalArgumentException("dm==null");
        }

        Border border = label.getBorder();
        if (border != null) {
            Insets insets = border.getBorderInsets(label);
            return new Dimension(dm.width + insets.left + insets.right,
                    dm.height + insets.top + insets.bottom);
        }
        return dm;
    }

    public static Component getHost() {
        if (host == null)
            return null;
        return host.get();
    }
}

Related

  1. adjustLabelSizes(List labels)
  2. getLabelPreferredSize(JLabel label)
  3. getPreferredLabelSize(JLabel c, int widthHint)
  4. makeSameSize(int alignment, JLabel... labels)
  5. resizeLabel(JLabel label, Dimension newDim)