public static String layoutCompoundLabel(FontMetrics fm,String text,Icon icon,int verticalAlignment,int horizontalAlignment,int verticalTextPosition,int horizontalTextPosition,Rectangle viewR,Rectangle iconR,Rectangle textR,int textIconGap){
  if (icon != null) {
    iconR.width=icon.getIconWidth();
    iconR.height=icon.getIconHeight();
  }
 else {
    iconR.width=iconR.height=0;
  }
  boolean textIsEmpty=(text == null) || (text.equals(""));
  String rettext="";
  if (textIsEmpty) {
    textR.width=textR.height=0;
  }
 else {
    Dimension dim=new Dimension(SwingUtilities.computeStringWidth(fm,text),fm.getHeight());
    textR.width=dim.width;
    textR.height=dim.height;
  }
  int gap=(textIsEmpty || (icon == null)) ? 0 : textIconGap;
  if (!textIsEmpty) {
    int availTextWidth;
    if (horizontalTextPosition == CENTER) {
      availTextWidth=viewR.width;
    }
 else {
      availTextWidth=viewR.width - (iconR.width + gap);
    }
    if (textR.width > availTextWidth) {
      String clipString="...";
      int totalWidth=SwingUtilities.computeStringWidth(fm,clipString);
      int nChars;
      rettext="";
      for (nChars=text.length() - 1; nChars >= 0; nChars--) {
        totalWidth+=fm.charWidth(text.charAt(nChars));
        if (totalWidth > availTextWidth) {
          break;
        }
        rettext=text.charAt(nChars) + rettext;
      }
      rettext=clipString + rettext;
      textR.width=SwingUtilities.computeStringWidth(fm,rettext);
    }
  }
  if (verticalTextPosition == TOP) {
    if (horizontalTextPosition != CENTER) {
      textR.y=0;
    }
 else {
      textR.y=-(textR.height + gap);
    }
  }
 else   if (verticalTextPosition == CENTER) {
    textR.y=(iconR.height / 2) - (textR.height / 2);
  }
 else {
    if (horizontalTextPosition != CENTER) {
      textR.y=iconR.height - textR.height;
    }
 else {
      textR.y=(iconR.height + gap);
    }
  }
  if (horizontalTextPosition == LEFT) {
    textR.x=-(textR.width + gap);
  }
 else   if (horizontalTextPosition == CENTER) {
    textR.x=(iconR.width / 2) - (textR.width / 2);
  }
 else {
    textR.x=(iconR.width + gap);
  }
  int labelR_x=Math.min(iconR.x,textR.x);
  int labelR_width=Math.max(iconR.x + iconR.width,textR.x + textR.width) - labelR_x;
  int labelR_y=Math.min(iconR.y,textR.y);
  int labelR_height=Math.max(iconR.y + iconR.height,textR.y + textR.height) - labelR_y;
  int dx, dy;
  if (verticalAlignment == TOP) {
    dy=viewR.y - labelR_y;
  }
 else   if (verticalAlignment == CENTER) {
    dy=(viewR.y + (viewR.height / 2)) - (labelR_y + (labelR_height / 2));
  }
 else {
    dy=(viewR.y + viewR.height) - (labelR_y + labelR_height);
  }
  if (horizontalAlignment == LEFT) {
    dx=viewR.x - labelR_x;
  }
 else   if (horizontalAlignment == RIGHT) {
    dx=(viewR.x + viewR.width) - (labelR_x + labelR_width);
  }
 else {
    dx=(viewR.x + (viewR.width / 2)) - (labelR_x + (labelR_width / 2));
  }
  textR.x+=dx;
  textR.y+=dy;
  iconR.x+=dx;
  iconR.y+=dy;
  return rettext;
}

public static String layoutCompoundLabel ( FontMetrics fm , String text , Icon icon , int verticalAlignment , int horizontalAlignment , int verticalTextPosition , int horizontalTextPosition , Rectangle viewR , Rectangle iconR , Rectangle textR , int textIconGap ) { if ( icon != null ) { iconR . width = icon . getIconWidth ( ) ; iconR . height = icon . getIconHeight ( ) ; } else { iconR . width = iconR . height = 0 ; } boolean textIsEmpty = ( text == null ) || ( text . equals ( "" ) ) ; String rettext = "" ; if ( textIsEmpty ) { textR . width = textR . height = 0 ; } else { Dimension dim = new Dimension ( SwingUtilities . computeStringWidth ( fm , text ) , fm . getHeight ( ) ) ; textR . width = dim . width ; textR . height = dim . height ; } int gap = ( textIsEmpty || ( icon == null ) ) ? 0 : textIconGap ; if ( ! textIsEmpty ) { int availTextWidth ; if ( horizontalTextPosition == CENTER ) { availTextWidth = viewR . width ; } else { availTextWidth = viewR . width - ( iconR . width + gap ) ; } if ( textR . width > availTextWidth ) { String clipString = "..." ; int totalWidth = SwingUtilities . computeStringWidth ( fm , clipString ) ; int nChars ; rettext = "" ; for ( nChars = text . length ( ) - 1 ; nChars >= 0 ; nChars -- ) { totalWidth += fm . charWidth ( text . charAt ( nChars ) ) ; if ( totalWidth > availTextWidth ) { break ; } rettext = text . charAt ( nChars ) + rettext ; } rettext = clipString + rettext ; textR . width = SwingUtilities . computeStringWidth ( fm , rettext ) ; } } if ( verticalTextPosition == TOP ) { if ( horizontalTextPosition != CENTER ) { textR . y = 0 ; } else { textR . y = - ( textR . height + gap ) ; } } else if ( verticalTextPosition == CENTER ) { textR . y = ( iconR . height / 2 ) - ( textR . height / 2 ) ; } else { if ( horizontalTextPosition != CENTER ) { textR . y = iconR . height - textR . height ; } else { textR . y = ( iconR . height + gap ) ; } } if ( horizontalTextPosition == LEFT ) { textR . x = - ( textR . width + gap ) ; } else if ( horizontalTextPosition == CENTER ) { textR . x = ( iconR . width / 2 ) - ( textR . width / 2 ) ; } else { textR . x = ( iconR . width + gap ) ; } int labelR_x = Math . min ( iconR . x , textR . x ) ; int labelR_width = Math . max ( iconR . x + iconR . width , textR . x + textR . width ) - labelR_x ; int labelR_y = Math . min ( iconR . y , textR . y ) ; int labelR_height = Math . max ( iconR . y + iconR . height , textR . y + textR . height ) - labelR_y ; int dx , dy ; if ( verticalAlignment == TOP ) { dy = viewR . y - labelR_y ; } else if ( verticalAlignment == CENTER ) { dy = ( viewR . y + ( viewR . height / 2 ) ) - ( labelR_y + ( labelR_height / 2 ) ) ; } else { dy = ( viewR . y + viewR . height ) - ( labelR_y + labelR_height ) ; } if ( horizontalAlignment == LEFT ) { dx = viewR . x - labelR_x ; } else if ( horizontalAlignment == RIGHT ) { dx = ( viewR . x + viewR . width ) - ( labelR_x + labelR_width ) ; } else { dx = ( viewR . x + ( viewR . width / 2 ) ) - ( labelR_x + ( labelR_width / 2 ) ) ; } textR . x += dx ; textR . y += dy ; iconR . x += dx ; iconR . y += dy ; return rettext ; }
compute and return the location of the icons origin , the location of origin of the text baseline , and a possibly clipped version of the compound labels string . locations are computed relative to the viewr rectangle . this layoutcompoundlabel ( ) does not know how to handle leading / trailing values in horizontaltextposition ( they will default to right ) and in horizontalalignment ( they will default to center ) . use the other version of layoutcompoundlabel ( ) instead .
