How to resize JComboBox scroll size?

I have several JComboBoxes in my program. I want to resize the scroll bar and arrow buttons so that they are much wider. I need this because I want to use the program on a Windows tablet, and it's too small for a finger to work. Is there any way to do this?

enter image description here

JComboBox comboBox; comboBox = new JComboBox(list_apple_device.toArray()); comboBox.setSelectedItem(null); comboBox.setFont(schrift); comboBox.setBounds(1568, 329, 306, 43); comboBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub textField.setText(""+e.getItem()); } }); getContentPane().add(comboBox); 

What is my code.

+5
source share
2 answers

You can use UIManger to control the scrollbar width:

 UIManager.put("ScrollBar.width", new Integer(50)); 

You must execute this code before creating the combo box.

+4
source

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)); //set the size you wish 

enter image description here

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

+3
source

Source: https://habr.com/ru/post/1243213/


All Articles