Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one resize the scrollelements of a JComboBox?

I have a few JComboBoxes in my programm. I want to change the size of the scrollbar and the arrow button in the way that they are much wider. I need that because I want to use the programm on a Windows tablet and it is too small for a finger to work with. Is there any possibility to do that?

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);

That's my code.

like image 783
Nikita.M Avatar asked Feb 17 '16 11:02

Nikita.M


People also ask

What can we do with items in jcombobox?

The common operations we can do with items in the combo box are adding, removing, setting selected item, getting selected item, and getting total number of items. Here are some examples: Note that the addItem () method of the JComboBox class still works in case of using a custom model.

What is setmaximumrowcount in jcombobox?

setMaximumRowCount (int count): sets the maximum number of rows the JComboBox displays. setEnabled (boolean b): enables the combo box so that items can be selected. removeItem (Object anObject) : removes an item from the item list. removeAllItems (): removes all items from the item list.

What is setselectedindex in jcombobox?

setSelectedIndex (int i): selects the element of JComboBox at index i. showPopup () :causes the combo box to display its popup window. setUI (ComboBoxUI ui): sets the L&F object that renders this component. setSelectedItem (Object a): sets the selected item in the combo box display area to the object in the argument.

What is setmodel and setpopupvisible in jcombobox?

setModel(ComboBoxModel<E> aModel) Sets the data model that the JComboBox uses to obtain the list of items. setPopupVisible(boolean v) Sets the visibility of the popup. setPrototypeDisplayValue(E prototypeDisplayValue) Sets the prototype display value used to calculate the size of the display for the UI portion.


2 Answers

You can use the UIManger to control the width of the scrollbar:

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

You would execute that code BEFORE you create the combo box.

like image 142
camickr Avatar answered Sep 22 '22 17:09

camickr


it's not so easy, but there is a solution, you have to subclass jcombobox...

You have to subclass JComboBox to get access to the ComboBoxUI. To do so you set your own custom ComboBoxUI during object instanciation (we make changes in the all constructors, see init() in CustomComboBox.

The ComboBoxUI is required to get access to the ComboboxPopup. We replace simply the default ComboboxPopup with our custom ComboboxPopup. You have to know that the ComboboxPopup is responsible for the creation of the drop-down-menu, that pops up when you click on the button.

then we finally can adjust the JScrollPane from the Popup, we grab the vertical JScrollBarand alter 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 the custom ComboboxUI that grants you acces to the ComboboxPopup (quite simple):

public class CustomComboBoxUI extends BasicComboBoxUI{

    protected ComboPopup createPopup() {
        return new CustomComboBoxPopup( comboBox );
    }

}

thankgod the custom ComboboxPopup needs just the basic constructor overriden and only one method changed (sets the size of the scrollpan to 40px):

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 combobox you simply 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 setting size of scroller and setting size of combobox for further help...

like image 26
Martin Frank Avatar answered Sep 19 '22 17:09

Martin Frank