Ellipses in JComboBox


I'm using a bunch of JComboBoxes, laid out with SpringUtilities.makeCompactGrid to make a form-like panel:

<Label1> <ComboBox1>
<Label2> <ComboBox2>


Selections in one combobox result in populating others.

The problem is, some of the data going into the combobox is very long, so it gets stretched out, sometimes outside the visible area.

I wanted to ellipsize Strings that would stretch out the combobox, but can't find a good way to do it.

Even if I set a max number of characters - which is a bad idea because comboboxes may be different widths on different panels - it doesn't work because in order to keeping things right sized on mac results in a lot of empty space on windows.

Any ideas?
Who is Participating?
believe it should get ellipsed for yuou automatically.
may be a problem with your layout

if you can prodice a small example that reproduces it I'll try it here

> Even if I set a max number of characters - which is a bad idea because comboboxes may be different widths on different panels

corrext, u don't want to do that
phavardelAuthor Commented:
Here's a little example:

 * Main.java
 * Created on May 30, 2006, 9:13 PM
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.

package springframe;

import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

 * @author tempta
public class SpringFrame extends JFrame implements ItemListener {
    JLabel label1;
    JLabel label2;
    JComboBox combo1;
    JComboBox combo2;
    JPanel subPanel;
    public SpringFrame() {
      subPanel = new JPanel(new SpringLayout());
      label1 = new JLabel("One:");
      label2 = new JLabel("Two:");
      combo1 = new JComboBox();
      combo2 = new JComboBox();
                                  subPanel.getComponentCount() / 2, 2,
                                  5, 5,
                                  5, 5);

    public static void main(String[] args) {
      SpringFrame a = new SpringFrame();

    public void itemStateChanged(ItemEvent e) {
      combo2.addItem("Very very very very very very very very long string");
setting the preferred size could help

combo2 = new JComboBox();
combo2.setPreferredSize(new Dimension(0,0));
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.