Results 1 to 6 of 6
  1. #1
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default [SOLVED] Item too big for JComboBox

    Is there a simple way to prevent the JComboBox from resizing in order to display a really long value? I have a combo box that displays the absolute path of a given file. The problem is that paths can get rather long. The panel my combo box is placed in has a minimum size, but no maximum size. So when the user resizes the window, it's possible that the combo box will grow along with the window.

    I'd like the combo box to stay within the bounds of the container it's in. Maybe display elipses (...) and when the user hovers over the item, the full text is displayed as a hint. Any ideas?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    You could try setPrototypeDisplayValue or setPreferredSize/setMaximumSize (which is used depends on your layout manager, which you haven't told us about).

    To get better help sooner, post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that clearly demonstrates your problem. Link:
    SSCCE : Java Glossary

    And post the code in code tags.

    db
    Last edited by DarrylBurke; 02-22-2009 at 03:41 PM.

  3. #3
    jason_check is offline Member
    Join Date
    Feb 2009
    Posts
    10
    Rep Power
    0

    Default

    You may set the layout to null and use setbound().
    View, validate and edit X9.37, X9.100-180 and UCD ICL files.
    http://www.digertech.com

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    To the OP, you're far better off going with Darryl's suggestion of changing the component's preferred size or maximum size than forgoing layout managers entirely as suggested by Jason. Neither of these suggestions will show ellipses (...) by default, nor will they show the entire String on mouse hover. To do that, you'll likely need to do some fancy coding including creating a new renderer and a mouse listener that shows a tooltip or a small non-decorated window. Needless to say this will take a bit of work I think.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Displaying tooltips is not too fancy and there's no need for a mouse listener. setToolTipText can be invoked from within the renderer code. Getting ellipses in the values displayed in the drop-down list may be a little fancy though. Im thinking SwingUtilities#layoutCompoundLabel.

    EDIT: done, but I sure hope there's a better way ;)
    Java Code:
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.Rectangle;
    import javax.swing.*;
    
    public class ShortCombo {
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
    
             @Override
             public void run() {
                new ShortCombo().makeUI();
             }
          });
       }
    
       public void makeUI() {
          Object[] data = {
             "Longish Data one",
             "Longish Data two",
             "Longish Data three",
             "Longish Data four",
             "Longish Data five"
          };
          final JComboBox combo = new JComboBox(data);
          combo.setPrototypeDisplayValue("Short");
          combo.setRenderer(new DefaultListCellRenderer() {
    
             @Override
             public Component getListCellRendererComponent(JList list, Object value,
                     int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index,
                        isSelected, cellHasFocus);
                if (index == -1) {
                   combo.setToolTipText(value.toString());
                   return this;
                }
                
                setToolTipText(value.toString());
                Rectangle textRect =
                        new Rectangle(combo.getSize().width,
                        getPreferredSize().height);
                String shortText = SwingUtilities.layoutCompoundLabel(this,
                        getFontMetrics(getFont()),
                        value.toString(), null,
                        getVerticalAlignment(), getHorizontalAlignment(),
                        getHorizontalTextPosition(), getVerticalTextPosition(),
                        textRect, new Rectangle(), textRect,
                        getIconTextGap());
                setText(shortText);
                return this;
             }
          });
          JFrame frame = new JFrame("");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(200, 200);
          frame.setLayout(new FlowLayout());
          frame.setLocationRelativeTo(null);
          frame.add(combo);
          frame.setVisible(true);
       }
    }
    db
    Last edited by DarrylBurke; 02-23-2009 at 08:21 AM.

  6. #6
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default

    Thanks everyone. My problem was that I had my container in a scrollpane and didn't notice that the horizontal scrollbar was there. Looks like combo boxes automatically implement the ellipses feature when the layout manager forces the box to be smaller than its contents.

Similar Threads

  1. Checking toggle state of a menu item
    By xsive in forum SWT / JFace
    Replies: 1
    Last Post: 09-22-2008, 02:42 PM
  2. Inserting a SWT tree item at an index
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:55 PM
  3. Detecting Mouse Down In SWT Tree Item
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:51 PM
  4. grabbing an item from an arraylist.
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 08:25 PM
  5. linked list nodes all refernce same item.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-18-2007, 08:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •