it is not so simple, but there is a solution, you have to subclass jcombobox ...
To access ComboBoxUI you need to subclass JComboBox . To do this, you install your own ComboBoxUI when initializing the object (we make changes to all the constructors, see init() in CustomComboBox ).
ComboBoxUI is required to access ComboBoxUI . Instead of the standard ComboboxPopup we are replacing our custom ComboboxPopup . You should be aware that ComboboxPopup is responsible for creating the drop-down menu that appears when you click the button.
then we can finally adjust the JScrollPane from the popup, we will take the vertical JScrollBar and change its appearance (setting a custom width).
public class CustomComboBox<T> extends JComboBox<T> { public CustomComboBox() { super(); init(); } public CustomComboBox(ComboBoxModel<T> aModel) { super(aModel); init(); } public CustomComboBox(T[] items) { super(items); init(); } public CustomComboBox(Vector<T> items) { super(items); init(); } public void init(){ CustomComboBoxUI ccbui = new CustomComboBoxUI(); setUI(ccbui); } }
this is a custom ComboBoxUI that gives you access to ComboboxPopup (pretty simple):
public class CustomComboBoxUI extends BasicComboBoxUI{ protected ComboPopup createPopup() { return new CustomComboBoxPopup( comboBox ); } }
thankgod custom ComboboxPopup needs only a basic constructor, and only one method has changed (sets the scroll size to 40 pixels):
public class CustomComboBoxPopup extends BasicComboPopup{ public CustomComboBoxPopup(JComboBox combo) { super(combo); } @Override protected void configureScroller() { super.configureScroller(); scroller.getVerticalScrollBar().setPreferredSize(new Dimension(40, 0)); } }
to set the size of the dropdown you just need to adjust its size
String[] data = new String[]{"a","b","c","d","e","f","g","h","i"}; CustomComboBox<String> comboBox = new CustomComboBox(data); comboBox.setPreferredSize(new Dimension(50,50));

see also scroller size setting and combobox parameter value for further help ...