+1  A: 

mmHhh perhaps providing a custom renderer?... .

I think it's listcell renderer or something like that.

For what I understand, the default is being wrapped when there is no enough space available, by creating/or modifying the cell rendered you can know what's the component available width and modify the text to be rendered at your will.

mmmhh I have not been close from that API ( the cell render stuff ) for a couple of years, so I could be probably a bit confused.

:)

OscarRyz
I'll give that a whirl this weekend; assuming it works, would you mind if I edit your answer with the renderer code and then accept it?
Software Monkey
Not at all, It would be an honor for me :)... if it works .... :P
OscarRyz
You might have problems with the JScrollPane if you're using it though :(
OscarRyz
Oscar, decided to post the solution as a separate self-answer in order to accept it so it shows at the top of the answers for future viewers looking for the solution; have voted up your answer since it was that which put me on the right track.
Software Monkey
A: 

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);
    }
}
Software Monkey