Hello,

I tried to use popup Menu in Glasspane but found some problem. When I right click on the frame the popup menu shown. If the popup menu intersected with the frame border, it worked fine. if the popup menu were inside the frame totally, I could not select the menu items. I tried to add "popup.setLightWeightPopupEnabled(false);". It did not do any good. The pouup menu worked fine if it intersected the frame border as before. But it would not show if the popup menu were inside the frame totally. Is there any way to fixed this?

Thank you very much.

jeyf


The following is the program:

import javax.swing.JComponent;
import javax.swing.JPopupMenu;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;

public class RightClickGlassPane1 extends JPanel {

public RightClickGlassPane1(JPopupMenu menu) {
addMouseListener(new PopupListener(menu));
}

public void paint(Graphics g) {
}

public static void main(String[] args) {
// create a frame with some components in it
JFrame frame = new JFrame("Right Click Test");
JLabel label = new JLabel("this is something under glasspane");
JPanel panel = new JPanel();
panel.setLayout(new java.awt.BorderLayout());
panel.add(label, java.awt.BorderLayout.CENTER);
frame.getContentPane().add(panel);

JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Dogs"));
popup.add(new JMenuItem("Cats"));
popup.add(new JMenuItem("Mass Hysteria"));
//popup.setLightWeightPopupEnabled(false);
JPanel jpanel1 = new JPanel(), jpanel2 = new JPanel();
jpanel1.add(new JButton("try1"));
jpanel2.add(new JButton("try2"));

// create the right-click glass pane.
JPanel rc = new RightClickGlassPane1(popup);

rc.setOpaque(false);
rc.setLayout(new java.awt.BorderLayout());
rc.add(jpanel1, java.awt.BorderLayout.EAST);
rc.add(jpanel2, java.awt.BorderLayout.CENTER);

// set as glasspane and make it visible
frame.setGlassPane(rc);
rc.setVisible(true);

// pack and show the frame
frame.pack();
frame.setSize(400, 200);
frame.setVisible(true);

}

class PopupListener extends MouseAdapter {

JPopupMenu popup;

PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}

public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()){
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
}