Here you can find the source of getClippedText(String text, FontMetrics fm, int maxWidth)
public static String getClippedText(String text, FontMetrics fm, int maxWidth)
//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; } }