How can one resize the scrollelements of a JComboBox?
Asked Answered
D

2

5

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.

Delisadelisle answered 17/2, 2016 at 11:51 Comment(4)
Side-note: This is not JavaScript, Java is type-safe. You can just call the toString() on your getItem() rather than creating string on the fly. Each time you concatenate strings, you are populating the string pool.Forestall
Hm, maybe JavaFX would be a better way to work with a Windows tablet? Because in JavaFX you can use CSS to style the elements: #21380365 ... seems like if the combobox is too small, other problems might arise which JavaFX could fix easier?Wistful
Have a look at the following discussion: #20996035Navarrete
Have a look at the following discussion: #20996035Navarrete
S
4

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.

Steelyard answered 17/2, 2016 at 16:12 Comment(4)
wow that's so much better ^^ - i'm really sorry, now i have to kill you ^_^ nothing personal =) (+1)Combat
Interesting, but won't that change the width of any scrollbar in the program? Or could you change that before creating the comboboxes, and put the previous value back after creating the comboboxes to retain the default scrollbar width?Sp
@Sp won't that change the width of any scrollbar in the program? - that would be the requirement in this case. ...and put the previous value back after creating the comboboxes - my standard response to a question like that is, try it! Its a single line of code that you can add to any program. I tried it with this example: https://mcmap.net/q/2034749/-binding-comboboxes-in-swing/… and was able to create two combo boxes with different widths. I didn't try it with a JList or JTable etc.Steelyard
@Sp i tried it out as well - you can in best way first get the value from the UIManager: Object originalValue = UIManager.get("ScrollBar.width"); and later rest the value to the original!Combat
C
3

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...

Combat answered 17/2, 2016 at 13:10 Comment(1)
seems like i forgot a lot of annotations @Override i'm sure you can add them on yourself ^^Combat

© 2022 - 2024 — McMap. All rights reserved.