Working on the idea from Oscar of using a ListCellRenderer, I have come up with something that almost works... The component value is rendered correctly, but the list's values require an ugly hack.
The hack is needed because for the list items the renderer's size (from getSize()) reflects the text width of the longest item, not the width of the space available to render the value. I tried use the JComboBox itself, but it's width includes the little drop-down button, so if there's a scrollbar present it's width is not accounted for. The hack is to store the renderer's width if it is less than the width of the combo-box and use the stored width if the renderer's width is greater than the width of the combo box. This has a corner case where the renderer's width is between that of the internal JLabel and the width of the combo-box.
Since the rendering space will be the width of the combo-box, less the width of a scroll bar and insets, if anyone has a suggestion as to how I can know the list has a scroll bar and how to get the scrollbar so I can extract the width, I am all ears. Maybe I can do list.getParent() and expect it to be a JScrollPane (either the JComboBox or JList doco does state that it uses a scroll pane).
Other suggestions to do this better are welcome.
Code follows:
recentDirs.setRenderer(new ComboTextRenderer(recentDirs));
...
static private class ComboTextRenderer
extends DefaultListCellRenderer
implements SwingConstants
{
JComponent parent;
int renderWidth;
ComboTextRenderer(JComponent par) {
super();
parent=par;
renderWidth=-1;
}
public void paint(Graphics gc) {
String txt=getText();
int len=txt.length();
int wid=getSize().width;
Insets ins=getInsets();
FontMetrics met=gc.getFontMetrics();
if(renderWidth==-1 || wid<parent.getSize().width) { renderWidth=wid; }
else { wid=renderWidth; }
wid-=(ins.left+ins.right);
if(met.stringWidth(txt)>wid) {
String rpl=null;
for(int xa=0,pfx=Math.min(15,((len/2)-1)),sfx=(pfx+2); pfx>0 && sfx<len; xa++) {
rpl=(txt.substring(0,pfx)+" ... "+txt.substring(sfx));
if(met.stringWidth(rpl)<=wid) { break; }
rpl=null;
if ((len-sfx)>15) { sfx++; }
else if((xa%2)==0 ) { pfx--; }
else { sfx++; }
}
if(rpl!=null) { setText(rpl); }
}
super.paint(gc);
}
}