Java Utililty Methods Swing Font Metrics

List of utility methods to do Swing Font Metrics

Description

The list of methods to do Swing Font Metrics are organized into topic(s).

Method

StringclipString(JComponent c, FontMetrics fm, String string, int availTextWidth)
Clips the passed in String to the space provided.
String clipString = "...";
int width = stringWidth(c, fm, clipString);
int nChars = 0;
for (int max = string.length(); nChars < max; nChars++) {
    width += fm.charWidth(string.charAt(nChars));
    if (width > availTextWidth)
        break;
string = string.substring(0, nChars) + clipString;
return string;
StringclipString(JComponent c, FontMetrics fm, String string, int availTextWidth)
Clips the passed in String to the space provided.
String clipString = "...";
int width = stringWidth(c, fm, clipString);
int nChars = 0;
for (int max = string.length(); nChars < max; nChars++) {
    width += fm.charWidth(string.charAt(nChars));
    if (width > availTextWidth) {
        break;
string = string.substring(0, nChars) + clipString;
return string;
StringclipStringIfNecessary(JComponent c, FontMetrics fm, String string, int availTextWidth)
Clips the passed in String to the space provided.
if ((string == null) || (string.equals(""))) {
    return "";
int textWidth = stringWidth(c, fm, string);
if (textWidth > availTextWidth) {
    return clipString(c, fm, string, availTextWidth);
return string;
...
StringclipText(FontMetrics fm, String val, int width)
Clip a text.
String str;
int i, size, totWidth;
totWidth = fm.stringWidth(CLIPPING);
size = val.length();
for (i = 0; i < size; i++) {
    totWidth += fm.charWidth(val.charAt(i));
    if (totWidth > width) {
        break;
...
DimensioncomputeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)
Computes the dimensions of a multi-line string using the specified font.
int nWidth, 
        nLines, 
        nI; 
if (oFontMetrics == null || astr == null)
    throw new IllegalArgumentException();
for (nI = 0, nWidth = 0, nLines = astr.length; nI < nLines; nI++)
    nWidth = Math.max(nWidth, SwingUtilities.computeStringWidth(oFontMetrics, astr[nI]));
return (new Dimension(nWidth, oFontMetrics.getHeight() * astr.length));
...
intcomputeStringWidth(FontMetrics fontMetrics, String str)
compute String Width
if (str == null || str.length() <= 0)
    return 0;
int width = 10 + javax.swing.SwingUtilities.computeStringWidth(fontMetrics, str);
int bytesLen = str.getBytes().length;
if (bytesLen >= 10)
    width += (bytesLen - 10) * 2 + 5;
return width;
intdrawLine(Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic)
Draws one of the lines on a multiline label/button.
int x = rect.x;
if (text != null) {
    int width = SwingUtilities.computeStringWidth(fm, text);
    if (hAlign == SwingConstants.CENTER) {
        x = rect.x + ((rect.width - width) / 2);
    } else if (hAlign == SwingConstants.RIGHT) {
        x = rect.x + (rect.width - width);
    BasicGraphicsUtils.drawString(g, text, mnemonic, x, y);
return x;
intgetBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
Determine where to break the given text to fit within the the given span.
char[] txt = s.array;
int txtOffset = s.offset;
int txtCount = s.count;
int index = Utilities.getTabbedTextOffset(s, metrics, x0, x, e, startOffset, false);
for (int i = txtOffset + Math.min(index, txtCount - 1); i >= txtOffset; i--) {
    char ch = txt[i];
    if (Character.isWhitespace(ch)) {
        index = i - txtOffset + 1;
...
StringgetClippedText(String text, FontMetrics fm, int maxWidth)
get Clipped Text
if ((text == null) || (text.length() == 0)) {
    return "";
int width = SwingUtilities.computeStringWidth(fm, text);
if (width > maxWidth) {
    int totalWidth = SwingUtilities.computeStringWidth(fm, ELLIPSIS);
    for (int i = 0; i < text.length(); i++) {
        totalWidth += fm.charWidth(text.charAt(i));
...
StringgetClippedText(String text, FontMetrics fm, int maxWidth)
get Clipped Text
if ((text == null) || (text.length() == 0)) {
    return "";
int width = SwingUtilities.computeStringWidth(fm, text);
if (width > maxWidth) {
    int totalWidth = SwingUtilities.computeStringWidth(fm, ELLIPSIS);
    for (int i = 0; i < text.length(); i++) {
        totalWidth += fm.charWidth(text.charAt(i));
...