Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 193667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:31:47+00:00 2026-05-11T16:31:47+00:00

I have a JComboBox which contains an MRU list combo-box and a for a

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:31:48+00:00Added an answer on May 11, 2026 at 4:31 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a JDialog which contains a JComboBox and a panel underneath which
I have an editable JComboBox which contains a list of single letter values. Because
I have an AutomationElement A (MS UI Automation), which represents a combo box. I
I have a combo box that I bind to an observable collection, which gets
I have a JComboBox which has a list of midi files, I wonder if
I have set up my own user control which contains two ComboBoxes, each ComboBox
I have a parent form, which contains panels that calls different user control. For
I have a form which contains infinite number of options which user and add.
We have a Java Swing application which contains components like JTable, JCombobox, JTextArea and
I have a dilemma whereby I have a form which contains a number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.