Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JComboBox that resizes according to the container size

843807Jul 30 2010 — edited Nov 10 2010
Hello there,

I have a JComboBox inside a JPanel. The contents of the drop down list are dynamically generated. When the list items are long, the JComboBox extends past the border of the enclosing JPanel, so the button on the right is not visible, which is not good. I can't set the maximum size of the combo box statically because I don't know how big the enclosing container will be (and it can be resized).

The behaviour I would like to achieve is that the combo box resizes according to the size of the enclosing container, not according to the size of list elements. I will probably need a horizontal scroll bar in the drop-down list if the list items are too long to fit in the available width.

I have tried using different layout managers for the container and it does not seem to make a difference. SteppedComboBox does not do what I want either, because I want the drop down list the same width as the text field (otherwise it will extend past the right edge of the screen).

Could somebody advise on how can I get the behaviour I need? Here is the sample code. Here the combo box is directly inside a JDialog, but the behaviour is the same. When you click on the "Long/Short" button it switches between the list with long and short items. When the items are long, the combo box extends past the edge of JDialog.

Thanks.
Alla
package foo;

import java.awt.BorderLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;

public class MyJDialog extends JDialog {
    private JButton jButton1;
    private JComboBox jComboBox1;
    
    boolean isLong = false;
    String[] shortlist = {"aaa", "bbb", "ccc"};
    String[] longlist = {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
        "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"};

    public MyJDialog() {
        super();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jComboBox1.setEditable(true);
        jComboBox1.setModel(new DefaultComboBoxModel(shortlist));

        jButton1.setText("Long/Short");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1, BorderLayout.NORTH);
        add(jComboBox1, BorderLayout.SOUTH);
    }

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        if (isLong) {
            jComboBox1.setModel(new DefaultComboBoxModel(shortlist));
            isLong = false;
        } else {
            jComboBox1.setModel(new DefaultComboBoxModel(longlist));
            isLong = true;
        }
        jComboBox1.setSelectedItem(null);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 8 2010
Added on Jul 30 2010
9 comments
3,621 views