Java Utililty Methods FontMetrics

List of utility methods to do FontMetrics

Description

The list of methods to do FontMetrics are organized into topic(s).

Method

Stringabbreviate(String str, FontMetrics fm, int width)
Abbreviate a String by simply truncating it.
int lastblank = 0, nchars = 0, cumx = 0;
while (cumx < width && nchars < str.length()) {
    if (Character.isWhitespace(str.charAt(nchars))) {
        lastblank = nchars;
    cumx += fm.charWidth(str.charAt(nchars));
    nchars++;
if (nchars < str.length() && lastblank > 0) {
    nchars = lastblank;
return (nchars > 0 ? str.substring(0, nchars) : str);
StringabbreviateName(String str, FontMetrics fm, int width)
Abbreviate a String as a given name.
if (fm.stringWidth(str) > width)
    str = abbreviateName(str, false);
if (fm.stringWidth(str) > width)
    str = abbreviateName(str, true);
return str;
intadjustFontSizeToGetPreferredWidthOfLabel(JLabel jLabel, int initialWidth)
adjust Font Size To Get Preferred Width Of Label
int width = initialWidth;
int initialFontSize = jLabel.getFont().getSize();
FontMetrics metrics = jLabel.getFontMetrics(jLabel.getFont());
if (metrics.stringWidth(jLabel.getText()) > width) {
    for (int i = initialFontSize; i > 10; i--) {
        jLabel.setFont(
                new java.awt.Font(jLabel.getFont().getFontName(), 0, jLabel.getFont().getSize() - 1));
        metrics = jLabel.getFontMetrics(jLabel.getFont());
...
intalignRight(FontMetrics fm, String string, int align)
Aligns the text on the right
int msg_width = fm.stringWidth(string);
int result = align - msg_width - 2;
return result;
StringclipString(FontMetrics fm, String text, int availableWidth)
clip String
if (text == null) {
    return "";
if (fm.stringWidth(text) < availableWidth) {
    return text;
String dots = "...";
StringBuilder sb = new StringBuilder(text + dots);
...
StringclipString(FontMetrics metrics, int availableWidth, String fullText)
Clips string based on specified font metrics and available width (in pixels).
if (metrics.stringWidth(fullText) <= availableWidth)
    return fullText;
String ellipses = "...";
int ellipsesWidth = metrics.stringWidth(ellipses);
if (ellipsesWidth > availableWidth)
    return "";
String starter = "";
int w = fullText.length();
...
StringclipString(Graphics g, String t, int width)
_more_
if (width <= 0) {
    return "";
FontMetrics fm = g.getFontMetrics();
int length = t.length();
int maxAdvance = fm.getMaxAdvance();
if (length * maxAdvance < width) {
    return t;
...
String[]cutString(String text, FontMetrics fontMetrics, int maxWidth)
It returns an array of subchains of maximum length in pixels.
Vector strings = new Vector(0, 10);
text = text.trim();
while (!text.trim().equals("")) {
    int cutPos = text.length();
    while (fontMetrics.stringWidth(text.substring(0, cutPos)) > maxWidth)
        cutPos--;
    int sepPos = cutPos - 1;
    boolean foundSep = false;
...
booleanfitString(String s, Graphics2D g2, Rectangle2D rect)
fit String
return getFitStringScale(s, g2, rect) >= 1;
String[][]getAllStylesSameWidthsFontsFamillyName()
get All Styles Same Widths Fonts Familly Name
String[] fontFamilly = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
List<String> all = new ArrayList();
List<String> notAll = new ArrayList();
for (int i = 0; i < fontFamilly.length; i++) {
    if (isAllStylesSameWidths(fontFamilly[i])) {
        all.add(fontFamilly[i]);
    } else {
        notAll.add(fontFamilly[i]);
...