Search code examples
javaswingstack-overflow

stackoverflow error when I add a glasspane to my frame


I am trying to add a universal right click to textfields in my application. I came across a solution where I could add a glass pane on top my frame, make it invisible and register that as a universal mouse listener. If the component is a text field I show the pop up menu, otherwise I redispatch the event. I have pasted the code below...

This example works fine. But when I use this with my application though, I get a stackoverflow error at

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at apple.awt.CWindow._getLocationOnScreen(Native Method)
at apple.awt.CWindow.getLocationOnScreen(CWindow.java:878)
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1960)
at java.awt.Component.getLocationOnScreen(Component.java:1938)
at javax.swing.SwingUtilities.convertPointToScreen(SwingUtilities.java:364)
at javax.swing.SwingUtilities.convertPoint(SwingUtilities.java:165)
at com.aesthete.csmart.ui.common.components.RightClickGlassPane.redispatchMouseEvent(RightClickGlassPane.java:79)
at com.aesthete.csmart.ui.common.components.RightClickGlassPane.mouseEntered(RightClickGlassPane.java:61)

I understand that every time the mouse is entered on a component the glass pane receives the event and then redispatches. But why is it getting converted into a recursive call?

EDIT: Just wanted to show everyone how I solved it with Camickr suggestion:

SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            final JPopupMenu popup = new JPopupMenu();
            JMenuItem mnItemCopy = new JMenuItem("Copy", CommonUI.getScaledImage(13, 13, "/images/copy.png"));
            JMenuItem mnItemCut = new JMenuItem("Cut", CommonUI.getScaledImage(13, 13, "/images/cut.png"));
            JMenuItem mnItemPaste = new JMenuItem("Paste", CommonUI.getScaledImage(13, 13, "/images/paste.png"));
            popup.add(mnItemCopy);
            popup.add(mnItemCut);
            popup.add(mnItemPaste);

            Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                @Override
                public void eventDispatched(AWTEvent event) {
                    if(event instanceof MouseEvent) {
                        MouseEvent mouseevent=(MouseEvent)event;
                        if(mouseevent.isPopupTrigger()) {
                            if (mouseevent.getComponent() instanceof JTextField) {
                                popup.show(mouseevent.getComponent(), mouseevent.getX(), mouseevent.getY());
                            }
                        }
                    }
                }
            }, AWTEvent.MOUSE_EVENT_MASK);
        }
    }); 

Solution

  • I am trying to add a universal right click to textfields in my application.

    Check out Global Event LIsteners. Just check the source of the event and do processing as required. No need to redispatch events.

    Note, you should NOT assume a right click is the LAF way to display a popup. Read the section from the Swing tutorial on Bringing Up a Popup Menu for a better solution.