public static String clipString(JComponent c,FontMetrics fm,String string,int availTextWidth){
  String clipString="...";
  availTextWidth-=SwingUtilities2.stringWidth(c,fm,clipString);
  if (availTextWidth <= 0) {
    return clipString;
  }
  boolean needsTextLayout;
synchronized (charsBufferLock) {
    int stringLength=syncCharsBuffer(string);
    needsTextLayout=isComplexLayout(charsBuffer,0,stringLength);
    if (!needsTextLayout) {
      int width=0;
      for (int nChars=0; nChars < stringLength; nChars++) {
        width+=fm.charWidth(charsBuffer[nChars]);
        if (width > availTextWidth) {
          string=string.substring(0,nChars);
          break;
        }
      }
    }
  }
  if (needsTextLayout) {
    AttributedString aString=new AttributedString(string);
    if (c != null) {
      aString.addAttribute(TextAttribute.NUMERIC_SHAPING,c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
    }
    LineBreakMeasurer measurer=new LineBreakMeasurer(aString.getIterator(),BreakIterator.getCharacterInstance(),getFontRenderContext(c,fm));
    string=string.substring(0,measurer.nextOffset(availTextWidth));
  }
  return string + clipString;
}

public static String clipString ( JComponent c , FontMetrics fm , String string , int availTextWidth ) { String clipString = "..." ; availTextWidth -= SwingUtilities2 . stringWidth ( c , fm , clipString ) ; if ( availTextWidth <= 0 ) { return clipString ; } boolean needsTextLayout ; synchronized ( charsBufferLock ) { int stringLength = syncCharsBuffer ( string ) ; needsTextLayout = isComplexLayout ( charsBuffer , 0 , stringLength ) ; if ( ! needsTextLayout ) { int width = 0 ; for ( int nChars = 0 ; nChars < stringLength ; nChars ++ ) { width += fm . charWidth ( charsBuffer [ nChars ] ) ; if ( width > availTextWidth ) { string = string . substring ( 0 , nChars ) ; break ; } } } } if ( needsTextLayout ) { AttributedString aString = new AttributedString ( string ) ; if ( c != null ) { aString . addAttribute ( TextAttribute . NUMERIC_SHAPING , c . getClientProperty ( TextAttribute . NUMERIC_SHAPING ) ) ; } LineBreakMeasurer measurer = new LineBreakMeasurer ( aString . getIterator ( ) , BreakIterator . getCharacterInstance ( ) , getFontRenderContext ( c , fm ) ) ; string = string . substring ( 0 , measurer . nextOffset ( availTextWidth ) ) ; } return string + clipString ; }
clips the passed in string to the space provided . note : this assumes the string does not fit in the available space .
