Search code examples
javaswingjcomboboxpreferredsize

Java Swing - Combobox with three dots


I have a jComboBox in my Swing GUI, filling the value from my SQL Database.

All looks fine but when I run the application, my combobox appeared e.g [Black ...] with the 3 dots to the side.

Please advice how can I remove the 3 dots.

Edited: Added code for my jComboBox and a image of it

    public void fillTankDepth()
{

    String fill = "Select * from swdepth";
    try{
        ps = conn.prepareStatement(fill);
        rs = ps.executeQuery();

        while(rs.next()){
            String depth = rs.getString("depth");
            comboDepth.addItem(depth);

                        }
    }
    catch(Exception e){
    JOptionPane.showMessageDialog(null, e);    
    }
 }

enter image description here

As per the image above, I've given quite a long width to it yet I'm unable to remove the 3 dots.


Solution

  • You likely aren't providing enough physical room on your GUI to display the data. Make more horizontal room for your combobox (i.e., make it longer).

    UPDATE

    As per the image above, I've given quite a long width to it yet I'm unable to remove the 3 dots.

    Looking at the screenshot you added, my guess is that it's a data problem. Some jdbc drivers will give you space-padded strings out to the length of the column, even if the data is much shorter. For example, a column defined as char(10) that has the value "A" in it may come back from your query as

    "A         "
    . If this is the case, trim the strings before adding them to the combo:

    String depth = rs.getString("depth").trim();
    

    Also, as suggested by mKorbel, you can set the prototype for your combo:

    // if you're using strings, and the max length will be 10
    comboDepth.setPrototypeDisplayValue("1234567890");
    

    Keep in mind, the padded spaces (as your screenshot suggests) will still be a problem if the spaces are causing the data to be longer than what you're expecting.