0

I have next structure: JPopupMenu contains JPanel which contains JMenuItems. The problem is, I cannot use it because JPopupMenu disappears when mouse enters to any menu item.

SSCCE:

public class PopupTest {
    public static void main(String[] a) {
        final JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createLineBorder(Color.RED));

        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    final JPopupMenu menu = new JPopupMenu();
                    JPanel menuPanel = new JPanel();
                    menuPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
                    menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));

                    for (int i = 0; i < 10; i++) {
                        JMenuItem item = new JMenuItem(String.valueOf(i));
                        menuPanel.add(item);
                    }

                    menu.add(menuPanel);
                    menu.show(panel, e.getX(), e.getY());
                }
            }
        });
        frame.setContentPane(panel);
        frame.setUndecorated(true);
        frame.setBackground(new Color(50, 50, 50, 200));

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.setVisible(true);
            }
        });
    }
}

BTW, problem does not repeats when there is no JPanel between JPopupMenu and its items.

Does anyone know how to prevent that?

SeniorJD
  • 6,946
  • 4
  • 36
  • 53
  • sure, don't use the panel ;-) Or if you think you need it: describe the requirements that forces you. MenuItems that don't reside in a hierarchy of menus are problematic, as you experience. An alternative to not using the intermediate panel, don't use menu/items in the hierarchy. – kleopatra Oct 21 '13 at 23:14

2 Answers2

1

Not directly answering your question but I think, you are unnecessarily adding a panel with box layout to JPopupMenu when it supports adding JMenuitem directly to it. check the following code fragment:

                 final JPopupMenu menu = new JPopupMenu();
                   JPanel menuPanel = new JPanel();
                    menuPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
                  //  menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));

                    for (int i = 0; i < 10; i++) {
                             JLabel item = new JLabel(i+"");
                             menuPanel.add(item);
                            }

                    menu.add(menuPanel);

                    menu.show(panel, e.getX(), e.getY());
Sage
  • 15,290
  • 3
  • 33
  • 38
  • problem is, i **NEED** to use the `JPanel` – SeniorJD Oct 09 '13 at 15:09
  • 1
    +1, I agree with Sage, just add the menu items directly to the menu. Why do you think you need to use a JPanel? – camickr Oct 09 '13 at 15:20
  • yes i think so too. However @SeniorJD, using JLabel instead of JMenuItem will resolve the issue as i have edited in the answer. – Sage Oct 09 '13 at 15:24
  • But i do think you should use JPopupMenu with menuitem. JPopupMenu itself is a component and it has the power laying out it's content as much as any other JComponent. – Sage Oct 09 '13 at 15:42
  • I need `JPanel`, because I have to implement complicated structure inside popup. – SeniorJD Oct 09 '13 at 16:37
1

Some tips:

  • Define your pop up menu just once instead every time a button is pressed.

  • You need to override mouseReleased or mousePressed method: Bringing Up a Popup Menu

  • Use MouseEvent.isPopupTrigger to find out if pop up should be shown.

  • Add menuItems directly to menu and not to a JPanel

Suggested changes:

    final JPopupMenu menu = new JPopupMenu();
    menu.setLayout(new GridLayout(2,5)); // How do you can, for example, lay out your menu items horizontally in 2 rows?
    for (int i = 0; i < 10; i++) {
        JMenuItem item = new JMenuItem(String.valueOf(i));
        menu.add(item);
    }

    MouseListener mouseListener = new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            if(e.isPopupTrigger()){
                menu.show(panel, e.getX(), e.getY());
            }
        }
    };

    panel.addMouseListener(mouseListener);

Picture

enter image description here

dic19
  • 17,821
  • 6
  • 40
  • 69
  • I'm sorry, but...ARE YOU KIDDING ME?? This an SSCCE of that what I NEED to do. Thanks for your care about my code' clean, but I just described what I need. How do you can, for example, lay out your menu items horizontally in 2 rows? – SeniorJD Oct 09 '13 at 15:12
  • 1
    First of all there's no need to be rude. Second, if you need so why didn't you state that in your question? Third, see updated answer and you'll see you can layout items without using `JPanel`. – dic19 Oct 09 '13 at 15:29
  • 1. I'm sorry. 2. I've showed that in sscce. 3. ok. and what about 3 columns with separators, labels and different insets? – SeniorJD Oct 09 '13 at 16:35
  • 1
    @SeniorJD _what I NEED to do_ actually, no .. the SSCCE is about a problem introduced by a perceived solution to a requirement you don't describe. – kleopatra Oct 21 '13 at 23:13
  • I have described it actually. – SeniorJD Oct 22 '13 at 06:40