Java JLabel extendByIcon(JLabel label, Dimension dm)

Here you can find the source of extendByIcon(JLabel label, Dimension dm)

Description

extend By Icon

License

Open Source License

Declaration

public static Dimension extendByIcon(JLabel label, Dimension dm) 

Method Source Code

//package com.java2s;
/* //from  w ww  .j a  v a  2 s.  co m
 * The MIT License
 *
 * Copyright 2014 Kamnev Georgiy (nt.gocha@gmail.com).
 *
 * ??????? ????????? ?????????, ????????????, ?????, ?????????? ????? ??????? ???????????? 
 * ????????????? ? ??????????????? ???????????? (? ?????????? ?????????? "??????????? ????????????"), 
 * ????????????? ??????????? ???????????? ??? ???????????, ???????? ?????????????? ????? ?? 
 * ??????????????, ???????????, ?????????, ???????????, ??????????, ?????????????????, ?????????????????? 
 * ?/??? ??????? ????? ???????????? ?????????????, ????? ??? ? ?????, ??????? ??????????????????? 
 * ?????? ??????????? ????????????, ??? ??????????? ?????????? ????????:
 *
 * ??????????????? ???????? ? ?????? ????????? ?????? ???? ???????? ?? ???? ????? 
 * ??? ???????? ?????? ??????? ???????????? ?????????????.
 *
 * ????????? ????????????? ???????????? ???????????????? ????? ?????, ??? ?????? ????? ???????????, 
 * ????? ????????????? ??? ?????????????????, ????????, ??? ??? ???????????????? ????????????? ?????????? ????????????, 
 * ???????????? ?? ??? ????????????? ??????????????? ? ??????????????? ?????. ??? ? ?????? ??????? ??????? 
 * ??? ?????????????????? ??? ?????? ????????????????? ?? ?????? ? ??????????? ???????, ??????? 
 * ??? ?????? ???????????? ?? ??????????? ?????????????, ????????? ??? ??????, ?????????? ??, ??????? 
 * ????????? ??? ???????????? ? ????????????? ????????????? ??? ???????????????? ?????????????? ???????????? 
 * ??? ?????? ?????????? ? ????????????? ?????????????.
 */

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class Main {

    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);
        }
    }
}

Related

  1. addMessageLogger(JLabel t)
  2. addParameterRow(Container container, JLabel label, Component component)
  3. clear(JLabel... fields)
  4. ensureCustomBackgroundStored(JLabel comp)
  5. exibeMensagemTemporaria(final JLabel lblMensagem, String mensagem)
  6. findMaxLabelWidth(JLabel... jLabels)
  7. getLabelValue(JLabel lbl)
  8. getRoughStroke(final JLabel _jlbl_stroke)
  9. getStoredBackground(JLabel comp)