Can a text widget show overflow with the “…” in the middle of the text instead of the end?

StackOverflow https://stackoverflow.com/questions/787907

  •  16-09-2019
  •  | 
  •  

Question

I have a JComboBox which contains an MRU list combo-box and a for a directory tree panel. Together, the two form the left hand panel of my GUI (the MRU is above the tree panel) which is a JSplitPane, so the left panel is resizeable.

It has a problem in that the directory text is always longer than the required width to see that directory in the tree panel, so I have to size my left-hand pane much wider than is needed for the tree in order to stop the combo-box from shown a truncated directory name.

The combo-box is not very useful with the end of the filename truncated, but making the left-pane wide enough for the filename very often makes it obtrusively too wide for the overall window, esp. if not running maximized. And it's usually only the trailing part of the filename which is of interest. If I could only cause JComboBox to somehow show "start...end" instead of "start..." my problem would be solved.

Screen Shot http://www.freeimagehosting.net/uploads/da9810ed86.png


UPDATE: I have a solution which works (see self-answer below), but it's not perfect. If someone knows how I can improve it, that would be much appreciated.

Was it helpful?

Solution 2

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 its width includes the little drop-down button, so if there's a scrollbar present its 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);
    }
}

OTHER TIPS

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.

:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top