3

Is it possible to increase the height of a JComboBox?(not refering to the popup menu)

Have tried:

comboBox.setPreferredSize(new Dimension(200, 100));

and

Component[] comp = comboBox.getComponents();
for (int i = 0; i < comp.length; i++) {
    if (comp[i] instanceof JButton) {
        JButton btn = (JButton) comp[i];
        btn.setPreferredSize(new Dimension(200, 100));

    }
}

But no luck. Then I tried to fix the problem with layout managers:

JPanel panel = new JPanel(new GridBagLayout());
panel.setPreferredSize(new Dimension(100, 100));
GridBagConstraints c = new GridBagConstraints();
c.weighty = 1;
c.fill = GridBagConstraints.VERTICAL;
panel.add(cbox, c);

But this dose not seems to change the size of the JComboBox button.

public class ComboBoxFontChange extends JFrame {

    public ComboBoxFontChange() {

        // CREATE BOX
        JComboBox<String> cbox = new JComboBox<String>();
        cbox.setFont(cbox.getFont().deriveFont(30.0f));

        // TRY CHANGE SIZE: DOSE NOT WORK..
        cbox.setPreferredSize(new Dimension(200, 100));
        cbox.setSize(new Dimension(200, 100));
        cbox.setMinimumSize(new Dimension(200, 100));

        // TRY CHANGE SIZE ON BUTTON INSTEAD: DOSE NOT WORK..
        Component[] comp = cbox.getComponents();
        for (int i = 0; i < comp.length; i++) {
            if (comp[i] instanceof JButton) {
                JButton btn = (JButton) comp[i];
                btn.setPreferredSize(new Dimension(200, 100));
                btn.setSize(new Dimension(200, 100));
                btn.setMinimumSize(new Dimension(200, 100));
            }
        }
        cbox.addItem("Quarter");
        cbox.addItem("Nickel");
        cbox.addItem("Penny");
        JPanel panel = new JPanel();
        panel.add(cbox);

        getContentPane().add(panel);

    }

    public static void main(String[] args) {
        ComboBoxFontChange frame = new ComboBoxFontChange();
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
Grains
  • 950
  • 3
  • 16
  • 35

2 Answers2

4

Here:

JPanel panel = new JPanel();
panel.add(cbox);

By default JPanel has a FlowLayout as layout manager and this one doesn't honor the components preferred size. It just fits the components using their minimum possible size. As @alex2410 says in his comment you need to manage components size and position by using a proper Layout manager.

Also take a look to this topic: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • Thanks for reply. Added a layout manager try but with no luck. Any idea why? – Grains Dec 19 '13 at 14:26
  • @Grains I tried with `BorderLayout`, `BoxLayout` and `GridLayout` and all of them honor the combo box preferred size (all tried on Windows 8 but this must be platform independent). About the buttons size it has something to do with the layout manager that `JComboBox` uses to lay out its internal components (I suspect it uses `FlowLayout` as well) and that's why setting the buttons preferred size doesn't work. – dic19 Dec 19 '13 at 14:57
2

Take a look at this code. Using custom ListCellRenderer I set the preferred size the of the visible cell.

You can use the following code class CustomComboBox and just change the dimension of the getPreferredSize of the JLabel.

And then for your comboBox just set the renderer comboBox.setRenderer(newCustomComboBox());

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;

public class ComboBoxDemo extends JFrame {

    JComboBox cbo = new JComboBox(new String[] {"Hello, StackOverflow"});

    public ComboBoxDemo(){
        cbo.setRenderer(new CustomComboBox());
        add(cbo, BorderLayout.SOUTH);
        add(new JLabel("Hello"), BorderLayout.CENTER);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new ComboBoxDemo();
            }
        });
    }

    static class CustomComboBox extends JLabel implements ListCellRenderer {

        @Override
        public Component getListCellRendererComponent(
                JList list, 
                Object value, 
                int index, 
                boolean isSelected, 
                boolean cellHasFocus) {

            JLabel label = new JLabel(){
                public Dimension getPreferredSize(){
                    return new Dimension(200, 100);
                }
            };
            label.setText(String.valueOf(value));

            return label;
        }
    }        
}

enter image description here

enter image description here

ListCellRenderer javadoc | JComboBox/ListCellRenderer tutorial

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720