views:

35

answers:

2

Hi, I have this strange bug with my popup menu. It happens rarely and seemingly randomly. The problem is when I have a submenu in my JPopupMenu - when I select the submenu, main menu disappears and the submenu is painted incorrectly (it's like the buffer of main menu is painted over the submenu). I can still navigate it using keyboard.

Here are some screenshots: This is how it should look like

alt text

And this is what it looks like when the bug appears:

alt text

So that glitch on the second picture is where the submenu should've been.

My code for showing the menu

addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            show(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            show(e);
        }

        public void show(MouseEvent e) {
            if (e.isPopupTrigger()) {
                if (selectSongsAt(e.getPoint())) {
                    JPopupMenu popup = buildTableMenu();
                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        }
    });

It's a JTable, selectSongsAt() just selects rows at given location and buildTableManu() returns new JPopupMenu.

What could cause this? There are no exceptions thrown, it doesn't seem platform-related, so I have no idea how to narrow this down. Please help.

A: 

I'm not sure if it's going to help with your issue but normally the popup menu should be added that way:

table.setComponentPopupMenu(popup);
Guillaume
Thanks, but I need to select rows after a right click and your method does not allow for that. I've found a solution, will post it later.
tulskiy
A: 

Thanks to one of my users, I've found a problem.

Just in case someone is hacking with swing more than they should do - this is what happened: in one part of my program I show a popup message on a JProgressBar showing position when the user is moving the thumb of the progress bar. To do this, I create a Popup using a PopupFactory. Then, using mouse listeners, I show and hide the popup. After I call hide() I didn't set the popup object to null which may led to calling the hide() twice, or keeping the popup from gc - don't know exactly. But apparently this messed up JPopupMenu's popup mechanism.

Quote from Popup.hide() JavaDoc explains it better:

Hides and disposes of the Popup. Once a Popup has been disposed you should no longer invoke methods on it. A disposed Popup may be reclaimed and later used based on the PopupFactory. As such, if you invoke methods on a disposed Popup, indeterminate behavior will result.

tulskiy