JPopupMenu goes behind main JFrame after checking/unchecking its Items.

I've written the following stand-alone code, which basically contains a JFrame that includes a combination of JPopupMenu's and JMenu's. If user right-clicks on the JFrame, a JPopupMenu will popup, which contains a JMenu. This JMenu also includes another JPopupMenu that contains 4 JCheckBoxMenuItems.

The problem that I'm facing is that, after I select one JCheckBoxMenuItem, the popup menu containing them, will go behind the JFrame. How can I get it to stay in front? Here's my SSCCE. I need to turn this in at work by tomorrow at the latest. So, if anyone has time, your help will be very much appreciated. Thanks!

import java.awt.Color;

import java.awt.Point;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JButton;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JPopupMenu;

import javax.swing.SwingUtilities;

publicclass MyPopupTest{

/**

* @param args

*/

public MyPopupTest(){}

publicvoid generateMenu()

{

final JFrame frame =new JFrame("My Frame");

frame.setSize(300,300);

final JPopupMenu popupMenu =new JPopupMenu();

final JMenu menu =new JMenu("My Menu");

final JPopupMenu itemsPopupMenu =new JPopupMenu();

final JButton okButton =new JButton("OK");

final JCheckBoxMenuItem item1 =new JCheckBoxMenuItem("Item 1",true);

final JCheckBoxMenuItem item2 =new JCheckBoxMenuItem("Item 2",false);

final JCheckBoxMenuItem item3 =new JCheckBoxMenuItem("Item 3",true);

final JCheckBoxMenuItem item4 =new JCheckBoxMenuItem("Item 4",false);

itemsPopupMenu.add(item1);

itemsPopupMenu.add(item2);

itemsPopupMenu.add(item3);

itemsPopupMenu.add(item4);

itemsPopupMenu.add(okButton);

menu.add(itemsPopupMenu);

popupMenu.add(menu);

frame.addMouseListener(new MouseAdapter()

{

publicvoid mouseClicked(MouseEvent e)

{

if(SwingUtilities.isRightMouseButton(e))

{

frame.getContentPane().add(popupMenu);

popupMenu.setLocation(e.getPoint().x+50,e.getPoint().y);

popupMenu.setVisible(true);

}

}

});

popupMenu.addMouseListener(new MouseAdapter()

{

publicvoid mouseEntered(MouseEvent e)

{

itemsPopupMenu.setLocation(new Point(popupMenu.getWidth()+menu.getLocationOnScreen().x, menu.getLocationOnScreen().y));

itemsPopupMenu.setVisible(true);

}

});

item1.addMouseListener(new MouseAdapter()

{

publicvoid mousePressed(MouseEvent e)

{

popupMenu.setVisible(true);

}

publicvoid mouseEntered(MouseEvent e)

{

item1.setBackground(Color.PINK);

}

publicvoid mouseExited(MouseEvent e)

{

item1.setBackground(popupMenu.getBackground());

}

});

item2.addMouseListener(new MouseAdapter()

{

publicvoid mousePressed(MouseEvent e)

{

popupMenu.setVisible(true);

}

publicvoid mouseEntered(MouseEvent e)

{

item2.setBackground(Color.PINK);

}

publicvoid mouseExited(MouseEvent e)

{

item2.setBackground(popupMenu.getBackground());

}

});

item3.addMouseListener(new MouseAdapter()

{

publicvoid mousePressed(MouseEvent e)

{

popupMenu.setVisible(true);

}

publicvoid mouseEntered(MouseEvent e)

{

item3.setBackground(Color.PINK);

}

publicvoid mouseExited(MouseEvent e)

{

item3.setBackground(popupMenu.getBackground());

}

});

item4.addMouseListener(new MouseAdapter()

{

publicvoid mousePressed(MouseEvent e)

{

popupMenu.setVisible(true);

}

publicvoid mouseEntered(MouseEvent e)

{

item4.setBackground(Color.PINK);

}

publicvoid mouseExited(MouseEvent e)

{

item4.setBackground(popupMenu.getBackground());

}

});

okButton.addMouseListener(new MouseAdapter()

{

publicvoid mousePressed(MouseEvent e)

{

popupMenu.setVisible(false);

itemsPopupMenu.setVisible(false);

}

});

frame.setVisible(true);

}

publicstaticvoid main(String [] args)

{

MyPopupTest test =new MyPopupTest();

test.generateMenu();

}

}

[9034 byte] By [programmer_girla] at [2007-11-27 7:07:56]
# 1
You're going about this the wrong way. Menus are supposed to disappear as soon as they're clicked on; it's one of the fundamental laws of user interaction. If you want the checkboxes to remain visible after the user clicks on them, you should put them in a JDialog or JWindow.
uncle_alicea at 2007-7-12 18:59:23 > top of Java-index,Desktop,Core GUI APIs...
# 2

Quit multi-posting. This is your third posting on this topic. Others have no idea what has already been suggested in the other postings and are wasting their time reading the posting. Keep the discussion in a single posting.

In fact I already explained to you how you can prevent the popup menu from closing and gave you a simple example to work from. For those of you that are curious. If you add a JCheckBox to a JPopupMenu, instead of a JCheckBoxMenuItem, the popup will stay open.

I can't explain the behaviour of the posted code, but when you have a smiple working solution maybe its time to use it as the basis of your solution.

camickra at 2007-7-12 18:59:23 > top of Java-index,Desktop,Core GUI APIs...
# 3

I didn't say it wasn't possible to get a JPopupMenu to work the way the OP seems to want, just that it's the wrong tool for the job. I did read the other threads, and I think the best advice we can give the OP is to scrap everything and start over with a JDialog or JWindow instead of a JPopupMenu.

uncle_alicea at 2007-7-12 18:59:23 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks for your posts. I actually figured out the problem today. So, it's fixed. Thanks again!
programmer_girla at 2007-7-12 18:59:23 > top of Java-index,Desktop,Core GUI APIs...
# 5
Did you reply to your two other postings on this topic stating the solution has been found so people don't waste their time reading those postings?
camickra at 2007-7-12 18:59:23 > top of Java-index,Desktop,Core GUI APIs...
# 6
Yes!
programmer_girla at 2007-7-12 18:59:23 > top of Java-index,Desktop,Core GUI APIs...