Fix JComboBox width

Roman Rdgz picture Roman Rdgz · Oct 19, 2011 · Viewed 12.4k times · Source

I have a JComboBox with some options. When I make a selection at another component, I'm changing JComboBox contents. First I call the removeAllItems() method, and then I add one by one the Strings I need now.

The problem is, by default I had some options, and one of them was the larger text, so JComboBox got the width it needed to show that option correctly. When I change the JComboBox contents, that text option is gone, and now a smaller text is giving the width to the JComboBox, so when I change contents it gets smaller.

My first approach was calling myComboBox.setPreferredSize(myComboBox.getSize()) and then its dimensions are fixed, but not correctly: it gets a bit smaller in height and width. I think it is because I'm using Nimbus Look&Feel, and the dimensions I'm getting from the getSize() method are the ones given by the Java default Look%Feel.

I also tried myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth())) with same results.

How can I approach this problem?

I add a code example of how I'm using the Layout:

    private String[] comboEventDOutputStrings = { "Run", "Stop", "Pause", "Conditioning time", "Deposition time", "Equilibration time", "Measurement time"};
    private String[] comboEventDInputStrings = { "Run", "Stop", "Pause"};
    // The first String array is the default set of values. It's obvious that the JComboBox will get smaller
    // when I change to the second array of contents

        //...   
            JPanel pane = new JPanel(new GridBagLayout());
            JPanel jPanelExterno = new JPanel(new GridBagLayout());
            GridBagConstraints cExterna = new GridBagConstraints();
            GridBagConstraints c = new GridBagConstraints();
            Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
            jPanelExterno.setBorder(loweredetched);
            jPanelExterno.setName("");

            cExterna.fill = GridBagConstraints.BOTH;
            cExterna.anchor = GridBagConstraints.NORTH;
            cExterna.gridx = 0;
            cExterna.gridy = 0;
            cExterna.insets = new Insets(10,10,5,5);

                JPanel jPanel1 = new JPanel(new GridBagLayout());
                jPanel1.setBorder(loweredetched);
                jPanel1.setName("PIO 1");

                jCheckBox1 = new JCheckBox("Enable");
                jCheckBox1.setSelected(false);
                jCheckBox1.setName("1");
                jCheckBox1.addActionListener(new PIOCheckListener());

                c.fill = GridBagConstraints.BOTH;
                c.anchor = GridBagConstraints.NORTH;
                c.gridx = 0;
                c.gridy = 0;
                c.insets = new Insets(10,5,10,10);
                jPanel1.add(jCheckBox1, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                JLabel label1 = new JLabel("IO Type");
                jPanel1.add(label1, c);
                c.gridx++;
                c.insets = new Insets(5,5,5,10);

                combo1 = new JComboBox(comboIOTypeStrings);
                combo1.setEnabled(false);
                combo1.setSelectedIndex(0);
                combo1.setName("1");
                combo1.addActionListener (new PIOComboListener());
                jPanel1.add(combo1, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label2 = new JLabel("Active level");
                jPanel1.add(label2, c);
                c.gridx++;
                c.insets = new Insets(5,5,5,10);

                combo2 = new JComboBox(comboActiveLevelStrings);
                combo2.setEnabled(false);
                combo2.setSelectedIndex(0);
                jPanel1.add(combo2, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label3 = new JLabel("Event");
                jPanel1.add(label3, c);
                c.gridx++;
                c.insets = new Insets(5,5,10,10);

                combo3 = new JComboBox(comboEventDOutputStrings);
                combo3.setEnabled(false);
                combo3.setSelectedIndex(0);
                jPanel1.add(combo3, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label4 = new JLabel("Node");
                jPanel1.add(label4, c);
                c.gridx++;
                c.insets = new Insets(5,5,10,10);

                combo4 = new JComboBox(comboNodeStrings);
                combo4.setEnabled(false);
                combo4.setSelectedIndex(0);
                jPanel1.add(combo4, c);

            jPanelExterno.add(jPanel1, cExterna);

        pioDialog.add(pane);
        pioDialog.pack();
        pioDialog.setLocationRelativeTo(null);
        pioDialog.setVisible(true);
    //...
}   


    class PIOComboListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent a) {
            JComboBox cb = (JComboBox)a.getSource();
            JComboBox target1 = null;
            JComboBox target2 = null;
            JComboBox target3 = null;
            switch(Integer.parseInt(cb.getName())){
                case 1:
                    target1 = combo2;
                    target2 = combo3;
                    target3 = combo4;
                    break;
                default:
                    Register.debug("PIODialog error: No target for comboBoxes");
                    break;
            }

            if(cb.getSelectedIndex()==2){ //Analog input
                target1.setEnabled(false);
                target2.setEnabled(false);
                target3.setEnabled(false);
            }
            else{
                target1.setEnabled(true);
                target2.setEnabled(true);
                target3.setEnabled(true);
                target2.removeAllItems();
                if(cb.getSelectedIndex()==0){
                    for(int i=0; i<comboEventDOutputStrings.length; i++)
                        target2.addItem(comboEventDOutputStrings[i]);
                } else {
                    for(int i=0; i<comboEventDInputStrings.length; i++)
                        target2.addItem(comboEventDInputStrings[i]);
                }
            }

        }
    }

It's basically a GridBagLayout with 6 JPanel with a new GridBagLayout for each one of them. I just wrote jPanel1 here to simplify things. I hope it is not very messy.

Answer

Roman Rdgz picture Roman Rdgz · Oct 19, 2011

Finally I found an easy solution:

jComboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXX");

And works perfect for what I wanted