Code
final JButton arrowButton = getArrowButton(combo02);
combo02.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
int itb=0, ilr=0;
Insets insets = getInsets();
itb+=insets.top+insets.bottom; ilr+=insets.left+insets.right;
insets = combo02.getInsets();
itb+=insets.top+insets.bottom; ilr+=insets.left+insets.right;
int availableWidth = combo02.getWidth()-ilr;
if(index < 0) {
//@see BasicComboBoxUI#rectangleForCurrentValue
int buttonSize = combo02.getHeight()-itb;
if(arrowButton!=null) {
buttonSize = arrowButton.getWidth();
}
availableWidth -= buttonSize;
JTextField tf = (JTextField)combo02.getEditor().getEditorComponent();
insets = tf.getMargin();
availableWidth -= (insets.left + insets.right);
}
String cellText = (value!=null)?value.toString():"";
//blockquote
//@cite http://tips4java.wordpress.com/2008/11/12/left-dot-renderer/
//@title Left Dot Renderer
//@auther Rob Camick
//FontMetrics fm = getFontMetrics(getFont());
//if(fm.stringWidth(cellText) > availableWidth) {
// String dots = "...";
// int textWidth = fm.stringWidth(dots);
// int nChars = cellText.length() - 1;
// while(nChars > 0) {
// textWidth += fm.charWidth(cellText.charAt(nChars));
// if(textWidth > availableWidth) break;
// nChars--;
// }
// setText(dots+cellText.substring(nChars+1));
//}
//blockquote
// Unicode surrogate programming with the Java language
// http://www.ibm.com/developerworks/library/j-unicode/index.html
FontMetrics fm = getFontMetrics(getFont());
if (fm.stringWidth(cellText) > availableWidth) {
String dots = "...";
int textWidth = fm.stringWidth(dots);
int len = cellText.length();
int[] acp = new int[cellText.codePointCount(0, len)];
int j = acp.length;
for (int i = len; i > 0; i = cellText.offsetByCodePoints(i, -1)) {
int cp = cellText.codePointBefore(i);
textWidth += fm.charWidth(cp);
if (textWidth > availableWidth) {
break;
}
acp[--j] = cp;
}
setText(dots + new String(acp, j, acp.length - j));
}
return this;
}
});
References
No comments:
Post a Comment