Java Swing Font Width estimateWidth(Font font, String msg)

Here you can find the source of estimateWidth(Font font, String msg)

Description

Estimates the width of the message being printed using the given font.

License

Open Source License

Parameter

Parameter Description
font font used.
msg the message.

Return

width.

Declaration

public static int estimateWidth(Font font, String msg) 

Method Source Code


//package com.java2s;
// the terms of the GNU General Public License as published by the Free Software Foundation;

import javax.swing.*;

import java.awt.*;

public class Main {
    private static final JLabel FONT_LABEL = new JLabel();

    /**//from  w w w .j  a  v  a 2s.c  om
     * Estimates the width of the message being printed using the given font.
     *
     * @param font  font used.
     * @param msg   the message.
     *
     * @return width.
     */
    public static int estimateWidth(Font font, String msg) {
        return FONT_LABEL.getFontMetrics(font).stringWidth(msg);
    }
}

Related

  1. getEditor(String html, int width, Color transparentColor, Font font)
  2. getHTMLTextForWidth(JComponent comp, double bboxWth, String txt, Font font)
  3. getStringWidth(final Font aFont, final String aString)
  4. getStringWidth(Font font, String str)