0

I have a JComboBox contain 9 image items. How can I arrange them to 3*3?

I want my items arrange like this

I've tried google it for several days, but I don't know what is the keyword for this question. Any advice is appreciated. Thanks for reading my question.

Edit:

Thanks for everyone's feedback. I am making a map editor where I can put map element together. The editor. It is more intuitive to arrange the stone road 3*3. The user can easily know which elements match each other.

I am not necessarily using combo box. I've also consider using buttons, but I think that buttons will waste a lat of space. Because I will have more map elements in the future.

2 Answers2

1

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

You can access and change the orientation of the JList to wrap items:

Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
JList list = popup.getList(); 
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(3);

This will allow you to navigate through the items using the up/down keys.

To support navigation using the left/right keys you need to add additional Key Bindings to the combo box:

InputMap im = comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke("LEFT"), "selectPrevious");
im.put(KeyStroke.getKeyStroke("RIGHT"), "selectNext");

However, the popup is based on the width of the largest item added to the combo box. This will be a problem as you won't see all the items. The items will scroll as you use the keys to navigate, but you won't see all 9 items at one time.

To solve this problem you check out Combo Box Popup. It has features that allow you to control the size/behaviour of the popup. You would use:

BoundsPopupMenuListener listener = new BoundsPopupMenuListener(true, false);
comboBox.addPopupMenuListener( listener );
camickr
  • 321,443
  • 19
  • 166
  • 288
0

It is possible to do this by giving the JComboBox a custom UI. However that UI is, at least in my case, a mess, so only use it as a workaround and clearly mark it as such with inline documentation.

The following code is made for the default Swing Look and Feel (Metal). If you want to use another L&F you need to exchange the class the custom UI is extending and maybe do some other adjustements. This will get tricky for distribution specific L&F (may force you to use reflection and delegation).
(If you also want to center your images, see this answer.)

Code:
(Sorry for the wierd comment wrapping, don't know whats wrong with my eclipse.)

To set the UI:

comboBox.setUI(new WrapComboBoxUI(3)); // 3 columns

Source code of WrapComboBoxUI:

import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;

public class WrapComboBoxUI extends MetalComboBoxUI {

    private int columnCount;

    public WrapComboBoxUI() {
        this(0);
    }

    /**
     * @param columnCount
     *            the amount of items to render on one row. <br/>
     *          A value of 0 or less will cause the UI to fit as many items
     *            into one row as possible.
     */
    public WrapComboBoxUI(int columnCount) {
        this.columnCount = columnCount;
    }

    public static ComponentUI createUI(JComponent c) {
        return new WrapComboBoxUI();
    }

    @Override
    protected ComboPopup createPopup() {
        ComboPopup created = super.createPopup();
        try {
            if (created instanceof JPopupMenu) {
                JPopupMenu popup = (JPopupMenu) created;
                JScrollPane scroll = (JScrollPane) popup.getComponent(0);
                JList<?> elementList = (JList<?>) scroll.getViewport().getView();
                elementList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
                elementList.setVisibleRowCount(-1);

                new Thread() {
                    @Override
                    public void run() {
                        while (true) {
                            try {
                                int fixedWidth = -1;
                                if (columnCount > 0) {
                                    int width = elementList.getWidth() - elementList.getInsets().left - elementList.getInsets().right;
                                    fixedWidth = width / columnCount;
                                }
                                boolean changed = false;
                                if (fixedWidth < 0 && elementList.getFixedCellWidth() >= 0 || fixedWidth >= 0 && elementList.getFixedCellWidth() < 0) {
                                    // if changed from not fixed to fixed or
                                    // other way around
                                    changed = true;
                                } else if (fixedWidth > 0 && fixedWidth - elementList.getFixedCellWidth() > 1) {
                                    // if width itself changed, ignoring slight
                                    // changes
                                    changed = true;
                                }
                                final int width = fixedWidth;

                                // no need to loop again before this is done, so
                                // we wait
                                if (changed)
                                    SwingUtilities.invokeAndWait(() -> elementList.setFixedCellWidth(width));

                                sleep(100);
                            } catch (Throwable e) {
                                // ignored
                            }
                        }
                    };
                }.start();
            }
        } catch (Throwable e) {
            System.err.println("Failed to customize ComboBoxUI:");
            e.printStackTrace();
        }
        return created;
    }
}
Lahzey
  • 373
  • 5
  • 17