Here you can find the source of getPreferredSize(String html, boolean width, int prefSize)
public static java.awt.Dimension getPreferredSize(String html, boolean width, int prefSize)
//package com.java2s; //License from project: Open Source License import javax.swing.JLabel; import javax.swing.text.View; public class Main { static final JLabel resizer = new JLabel(); public static java.awt.Dimension getPreferredSize(String html, boolean width, int prefSize) { resizer.setText(html);//from ww w.j av a 2 s . com View view = (View) resizer.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey); view.setSize(width ? prefSize : 0, width ? 0 : prefSize); float w = view.getPreferredSpan(View.X_AXIS); float h = view.getPreferredSpan(View.Y_AXIS); return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h)); } }