setText(s);
FontMetrics fm = getToolkit().getFontMetrics(list.getFont()); int strWidth = SwingUtilities.computeStringWidth(fm, s); int cbWidth = getSize().width; if (cbWidth != 0 && strWidth > cbWidth) { char[] str = s.toCharArray(); int currWidth = 0; int i = 0; String postStr = "... "; cbWidth -= SwingUtilities.computeStringWidth(fm, postStr); while (i < str.length && currWidth < cbWidth) { ++i; currWidth = SwingUtilities.computeStringWidth(fm, new String(str, 0, i)); } setText(new String(str, 0, i)  + postStr); } else setText(s);
  public Component getListCellRendererComponent(JList list, Object value,                                                int index, boolean isSelected,                                                boolean cellHasFocus)  {    String s = value.toString();    setText(s);    setOpaque(true);    UIDefaults defaults = UIManager.getLookAndFeelDefaults();    if (isSelected)      {	setBackground(list.getSelectionBackground());	setForeground(list.getSelectionForeground());      }    else      {	setBackground(list.getBackground());	setForeground(list.getForeground());      }    setEnabled(list.isEnabled());    setFont(list.getFont());    // Use focusCellHighlightBorder when renderer has focus and     // noFocusBorder otherwise    if (cellHasFocus)      setBorder(UIManager.getBorder("List.focusCellHighlightBorder"));    else      setBorder(noFocusBorder);    return this;  }