I have a dialog box with a custom panel. The panel uses a GridBagLayout. How can I set the initial size of a JComboBox.
Asked
Active
Viewed 2,570 times
2 Answers
3
Try this method. Width of a combo box is automatically determined by the width of the largest item added. You can control this by using:
comboBox.setPrototypeDisplayValue("default text here");

Nishan
- 3,644
- 1
- 32
- 41
-
Also remember, that this will limit the size of the drop down box as well and elements which exceeded the width will simply be truncated – MadProgrammer May 13 '17 at 22:32
-1
if you want to set the initial size ofJComboBox
with out adding any items to it than u can use setBounds()
method here is the example
output image
import javax.swing.*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
JComboBox cb=new JComboBox();
cb.setBounds(50, 50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String[] args) {
new ComboBoxExample();
}
}

Pavan Baddi
- 479
- 1
- 11
- 22
-
2`f.setLayout(null);` Stop giving advice on Java GUI layouts until you can explain how foolish that advice is. To start your understanding.. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 13 '17 at 11:57