Ellipses in JComboBox

Hi,

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

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

etc.

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?
LVL 1
phavardelAsked:
Who is Participating?
 
objectsCommented:
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
0
 
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();
      
      combo1.addItem("blah");
      combo1.addItem("arg");
      combo1.addItemListener(this);
      
      subPanel.add(label1);
      subPanel.add(combo1);
      subPanel.add(label2);
      subPanel.add(combo2);
      
      SpringUtilities.makeCompactGrid(subPanel,
                                  subPanel.getComponentCount() / 2, 2,
                                  5, 5,
                                  5, 5);
      
      add(subPanel);
      
      
    }
   

    public static void main(String[] args) {
      SpringFrame a = new SpringFrame();
      a.setSize(100,100);
      a.setDefaultCloseOperation(EXIT_ON_CLOSE);
      a.setVisible(true);
    }

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

combo2 = new JComboBox();
combo2.setPreferredSize(new Dimension(0,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.