List of utility methods to do Swing Font Metrics
String[] | layoutMultilineCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap, int minLines, int maxLines) Lays out a multiline label/button with icon. String result[] = new String[maxLines]; if (icon != null) { iconR.width = icon.getIconWidth(); iconR.height = icon.getIconHeight(); } else { iconR.width = iconR.height = 0; boolean textIsEmpty = (text == null) || text.equals(""); ... |
int | stringWidth(JComponent c, FontMetrics fm, String string) Returns the width of the passed in String. return fm.stringWidth(string);
|
List | wrapString(String s, FontMetrics fm, int width) wrap String List<String> lines = new ArrayList<String>(); StringBuilder line = new StringBuilder(); for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); String test = line.toString() + ch; if (fm.stringWidth(test) > width) { if (test.length() > 1) { boolean breakFound = false; ... |
String | wrapText(String text, FontMetrics metrics, int maxWidth) Handy-dandy text wrapping function, adapted from http://www.geekyramblings.net/2005/06/30/wrap-jlabel-text/. BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(text); StringBuilder line = new StringBuilder(); StringBuilder paragraph = new StringBuilder(); for (int start = boundary.first(), end = boundary .next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { String word = text.substring(start, end); if (word.equals("\n")) ... |