1 19 20 package org.netbeans.modules.versioning.system.cvss.ui; 21 22 import javax.swing.*; 23 import javax.swing.plaf.basic.BasicHTML ; 24 import javax.swing.text.View ; 25 import java.awt.*; 26 27 32 public class UIUtils { 33 34 45 public static void computePreferredSize(JLabel label, int ex) { 46 assert ex > 0; 47 FontMetrics fm = label.getFontMetrics(label.getFont()); 48 StringBuffer sb = new StringBuffer (ex); 49 for (int i = 0; i<ex; i++) { 50 sb.append("x"); } 52 int exWidth = SwingUtilities.computeStringWidth(fm, sb.toString()); 53 View v = BasicHTML.createHTMLView(label, label.getText()); 54 v.setSize(exWidth, 0); 55 int width = (int) v.getPreferredSpan(View.X_AXIS); 56 int height = (int) v.getPreferredSpan(View.Y_AXIS); 57 58 Insets insets = label.getInsets(); 59 int dx = insets.left + insets.right; 60 int dy = insets.top + insets.bottom; 61 62 label.setPreferredSize(new Dimension(width+dx, height+dy)); 63 } 64 } 65 | Popular Tags |