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; /*/* ww w . j a va 2 s . c om*/ * DataViewerUtilities.java 2/6/13 1:04 PM * * Copyright (C) 2012-2013 Nick Ma * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 3 * of the License, or any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ 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; } }