How to create Combobox with multiselection?

18,660

Solution 1

There are a few basic problems with creating custom combobox popup content (like a list with multiselection):
1. Default UI suggests JList usage as the content so to change that behavior you will have to change the whole ComboBoxUI
2. You cannot simply change the default combobox list into multiselection one due to the fact that only one value gets "selected" at the end and list has default rollover selection mouse listener, that will make you unable to choose more than one element

So i'd reccomend you to use simple JList instead of combobox or look into using some extended components libraries like JideSoft - they have this component and lots more which you won't be able to quickly create using Swing features.

Solution 2

I know, that the question is rather old, but for those, who still looks for solution of this problem, try the following code:

public class ComboSelections {

public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, UnsupportedLookAndFeelException {

UIManager.setLookAndFeel((LookAndFeel) Class.forName("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel").newInstance());

final JPopupMenu menu = new JPopupMenu();
JMenuItem one = new JCheckBoxMenuItem("One");
JMenuItem two = new JCheckBoxMenuItem("Two");
JMenuItem three = new JCheckBoxMenuItem("Three");
JMenuItem four = new JCheckBoxMenuItem("Four");
menu.add(one);
menu.add(two);
menu.add(three);
menu.add(four);


final JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (!menu.isVisible()) {
            Point p = button.getLocationOnScreen();
            menu.setInvoker(button);
            menu.setLocation((int) p.getX(),
                    (int) p.getY() + button.getHeight());
            menu.setVisible(true);
        } else {
            menu.setVisible(false);
        }

    }
});

one.addActionListener(new OpenAction(menu, button));
two.addActionListener(new OpenAction(menu, button));
three.addActionListener(new OpenAction(menu, button));
four.addActionListener(new OpenAction(menu, button));

JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(button);
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static class OpenAction implements ActionListener {

    private JPopupMenu menu;
    private JButton button;

    private OpenAction(JPopupMenu menu, JButton button) {
        this.menu = menu;
        this.button = button;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        menu.show(button, 0, button.getHeight());
    }
}
}
Share:
18,660
manhnt
Author by

manhnt

on software

Updated on June 04, 2022

Comments

  • manhnt
    manhnt almost 2 years

    I need to create a combo box with multi-selection, how to achieve that?

  • Mikle Garin
    Mikle Garin about 12 years
    Its just that i have already tried to change combobox popup into a tree-like and i have failed - that attempt took almost a day. So that is why i recommend using JList OR as an option - JButton with popup containing any components/editors you like.
  • Denis
    Denis over 8 years
    nice solution of problem)