Change height of JCombobox
Solution 1
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?
Solution 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;
}
}
}
ListCellRenderer javadoc | JComboBox/ListCellRenderer tutorial
Grains
Updated on June 04, 2022Comments
-
Grains about 2 years
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); } }