Home > Net >  Grid selection popup for combo box
Grid selection popup for combo box

Time:11-26

I have a JComboBox that lets you select a profile image - a gallery of sorts.

An image of my current combo box:

https://img.codepudding.com/202111/0653dc3284d94dafb10b248f0d5964fb.png

You can already see how unwieldy this would be, should I add more images.

Essentially, I would like to "replace" the popup with a grid-based one, to allow for easier selection of an image.
I would like to keep the appearance of the combo box itself, however - showing details about the currently selected image.

The only way I can currently see to do this is to use a custom Look and Feel UI class, which AFAIK would make the combo box look out-of-place due to the rest of my app using the system L&F.

CodePudding user response:

The popup of the combo box uses a JList to display the items.

You can configure the JList to use horizontal wrapping:

BasicComboPopup popup = (BasicComboPopup)comboBox.getAccessibleContext().getAccessibleChild(0);
JScrollPane scrollPane = (JScrollPane)popup.getComponent(0);
JList list = (JList) scrollPane.getViewport().getView();
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(2);

Read the section from the Swing tutorial on Initializing a List for more information.

  • Related