Here you can find the source of getPreferredLabelSize(JLabel c, int widthHint)
public static Dimension getPreferredLabelSize(JLabel c, int widthHint)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 BSI Business Systems Integration AG. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w ww .j a v a 2 s.co m*/ * BSI Business Systems Integration AG - initial API and implementation ******************************************************************************/ import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Insets; import java.awt.Rectangle; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.text.View; public class Main { public static Dimension getPreferredLabelSize(JLabel c, int widthHint) { View v = (View) c.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey); if (v == null) { return c.getPreferredSize(); } // String text = c.getText(); Icon icon = c.isEnabled() ? c.getIcon() : c.getDisabledIcon(); int hAlign = c.getHorizontalAlignment(); int vAlign = c.getVerticalAlignment(); int hTextPosition = c.getHorizontalTextPosition(); int vTextPosition = c.getVerticalTextPosition(); int iconTextGap = c.getIconTextGap(); // int mnemonicIndex=c.getDisplayedMnemonicIndex(); Font font = c.getFont(); Insets insets = c.getInsets(new Insets(0, 0, 0, 0)); // int dx = insets.left + insets.right; int dy = insets.top + insets.bottom; if (icon == null && (text == null || font == null)) { return new Dimension(dx, dy); } else if ((text == null) || ((icon != null) && (font == null))) { return new Dimension((icon != null ? icon.getIconWidth() : 0) + dx, (icon != null ? icon.getIconHeight() : 0) + dy); } else { Rectangle iconR = new Rectangle(); Rectangle textR = new Rectangle(); Rectangle viewR = new Rectangle(); FontMetrics fm = c.getFontMetrics(font); iconR.x = iconR.y = iconR.width = iconR.height = 0; textR.x = textR.y = textR.width = textR.height = 0; viewR.x = dx; viewR.y = dy; viewR.width = widthHint - dx; viewR.height = Short.MAX_VALUE; //PATCH BEGIN: set inner size of html view int availTextWidth; int gap = (icon == null) ? 0 : iconTextGap; if (hTextPosition == SwingConstants.CENTER) { availTextWidth = viewR.width; } else { availTextWidth = viewR.width - (((icon == null) ? 0 : icon.getIconWidth()) + gap); } v.setSize(availTextWidth, 0); //PATCH END SwingUtilities.layoutCompoundLabel(c, fm, text, icon, vAlign, hAlign, vTextPosition, hTextPosition, viewR, iconR, textR, iconTextGap); int x1 = Math.min(iconR.x, textR.x); int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width); int y1 = Math.min(iconR.y, textR.y); int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height); Dimension rv = new Dimension(x2 - x1, y2 - y1); rv.width += dx; rv.height += dy; return rv; } } }