Java Swing Font Metrics getClippedText(String text, FontMetrics fm, int maxWidth)

Here you can find the source of getClippedText(String text, FontMetrics fm, int maxWidth)

Description

get Clipped Text

License

Open Source License

Declaration

public static String getClippedText(String text, FontMetrics fm,
            int maxWidth) 

Method Source Code

//package com.java2s;
/*/*from   w ww .  j  a  v  a2  s. c  o  m*/
 * Copyright 2005 MH-Software-Entwicklung. All rights reserved.
 * Use is subject to license terms.
 */

import java.awt.*;
import javax.swing.*;

public class Main {
    private static final String ELLIPSIS = "...";

    public static String getClippedText(String text, FontMetrics fm,
            int maxWidth) {
        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));
                if (totalWidth > maxWidth) {
                    return text.substring(0, i) + ELLIPSIS;
                }
            }
        }
        return text;
    }
}

Related

  1. computeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)
  2. computeStringWidth(FontMetrics fontMetrics, String str)
  3. drawLine(Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic)
  4. getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
  5. getClippedText(String text, FontMetrics fm, int maxWidth)
  6. getClippedText(String text, FontMetrics fm, int maxWidth)
  7. getDefaultLabelFontMetrics()
  8. getFontMetrics(final JComponent c, final Graphics g)
  9. getFontMetrics(Font font)