JComboBox on a JPopupMenu
Asked Answered
F

2

8

I'm trying to use a compound Swing component as part of a Menu.

Everything works just fine, apart from one detail: The component contains JComboBoxes and whenever the user clicks on one of them to open its dropdown, the dropdown opens but the menu disappears. Is it possible to make the menu stay open when a JComboBox is clicked?

I sub-classed JMenu. This is the corresponding code:

public class FilterMenu extends JMenu {

    public FilterMenu(String name) {
        super(name);

        final JPopupMenu pm = this.getPopupMenu();
        final FilterPanel filterPanel = new FilterPanel(pm) {
            @Override
            public void updateTree() {
                super.updateTree();
                pm.pack();
            }
        };
        pm.add(filterPanel);
    }
}

FilterPanel is the custom compound component. The pm.pack() is called to adapt the size of the JPopupMenu when the filterPanel changes in size.

Thanks for your help

Fideicommissum answered 2/7, 2011 at 19:52 Comment(2)
Wouldn't a submenu be more appropriate than a JComboBox?Ultrafilter
@Fideicommissum agreed with @Jesse Barnum subMenuItem +1Anchor
A
2

are you meaning this bug

import javax.swing.*;
import java.awt.event.*;

public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(400, 400);
        frame.setVisible(true);
        String[] list = {"1", "2", "3", "4",};
        JComboBox comb = new JComboBox(list);
        final JPopupMenu pop = new JPopupMenu();
        pop.add(comb);
        frame.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("mousePressed");
                pop.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }
}
Anchor answered 2/7, 2011 at 20:31 Comment(0)
U
1

Look at Jide OSS' PopupWindow. This provides an easy-to-use solution for this problem. Works fine for me.

Javadoc is here.

Ubana answered 22/5, 2012 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.