Java Swing Font Metrics clipString(JComponent c, FontMetrics fm, String string, int availTextWidth)

Here you can find the source of clipString(JComponent c, FontMetrics fm, String string, int availTextWidth)

Description

Clips the passed in String to the space provided.

License

Open Source License

Parameter

Parameter Description
c JComponent that will display the string, may be null
fm FontMetrics used to measure the String width
string String to display
availTextWidth Amount of space that the string can be drawn in

Return

Clipped string that can fit in the provided space.

Declaration

public static String clipString(JComponent c, FontMetrics fm, String string, int availTextWidth) 

Method Source Code

//package com.java2s;
/*/*from w  w  w . ja  v  a2  s  .co  m*/
 * Copyright (C) 2015 Jack Jiang(cngeeker.com) The BeautyEye Project. 
 * All rights reserved.
 * Project URL:https://github.com/JackJiang2011/beautyeye
 * Version 3.6
 * 
 * Jack Jiang PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * MySwingUtilities2.java at 2015-2-1 20:25:40, original version by Jack Jiang.
 * You can contact author with jb2011@163.com.
 */

import java.awt.FontMetrics;

import javax.swing.JComponent;

public class Main {
    /**
     * Clips the passed in String to the space provided. NOTE: this assumes the
     * string does not fit in the available space.
     *
     * @param c JComponent that will display the string, may be null
     * @param fm FontMetrics used to measure the String width
     * @param string String to display
     * @param availTextWidth Amount of space that the string can be drawn in
     * @return Clipped string that can fit in the provided space.
     */
    public static String clipString(JComponent c, FontMetrics fm, String string, int availTextWidth) {
        // c may be null here.
        String clipString = "...";
        int width = stringWidth(c, fm, clipString);
        // NOTE: This does NOT work for surrogate pairs and other fun
        // stuff
        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;
    }

    /**
     * Returns the width of the passed in String.
     *
     * @param c JComponent that will display the string, may be null
     * @param fm FontMetrics used to measure the String width
     * @param string String to get the width of
     * @return the int
     */
    public static int stringWidth(JComponent c, FontMetrics fm, String string) {
        return fm.stringWidth(string);
    }
}

Related

  1. clipString(JComponent c, FontMetrics fm, String string, int availTextWidth)
  2. clipStringIfNecessary(JComponent c, FontMetrics fm, String string, int availTextWidth)
  3. clipText(FontMetrics fm, String val, int width)
  4. computeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)