Ellipses in JComboBox

Posted on 2006-05-30
Last Modified: 2008-02-01

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?
Question by:phavardel
    LVL 92

    Accepted Solution

    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
    LVL 1

    Author Comment

    Here's a little example:

     * 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");
    LVL 14

    Assisted Solution

    setting the preferred size could help

    combo2 = new JComboBox();
    combo2.setPreferredSize(new Dimension(0,0));

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now