1

I am developing a Java Swing application that will have several panels in it, one of which is NASA's WorldWind. In this WorldWind panel I need to have a right click menu for various actions. I was successful in adding the right click menu and all is good, until the user resizes the window.

I am not sure what about that action would be causing the right click menu to stop working. The mouse listener is working fine still, but the menu no longer shows up. None of the objects get garbage collected, nothing seems to change.

Here is a quick sample of how my code is set up:

class ClickListener extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
        if (SwingUtilities.isRightMouseButton(e) == true) {
            showRightClickMenu(e);
        } else {
            addPoint(e);
        }
    }
}

void showRightClickMenu(MouseEvent e) {
        System.out.println("In Right Click");
        if (menu == null) {
            setupRightClickMenu();
        }
        menu.show(e.getComponent(), e.getX(), e.getY());
}

void setupRightClickMenu() {
    menu = new JPopupMenu("RightClick");
    JMenuItem button1 = new JMenuItem("Show Waypoint Table");
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // ptTable.showTable();
        }
    });
    menu.add(button1);
    JMenuItem button2 = new JMenuItem("Clear Waypoints");
    button2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            ptLayer.removeAllRenderables();

        }
    });
    menu.add(button2);
}

I can set the size to whatever I want at application launch and the right click works, but once I resize, it fails. Any ideas?

UPDATE

Actually after playing with it a bit more and talking with someone else I am working with, it looks like it may be drawing the menu behind the WorldWind canvas. I am not sure why this is, but I need to make sure it shows in front of it.

I tried changing the JPopupMenu property talked about here, but that does not seem to help.

Community
  • 1
  • 1
MZimmerman6
  • 8,445
  • 10
  • 40
  • 70
  • Do you do "things" when the window is resized ? – Xvolks Mar 03 '16 at 19:15
  • Nope. I do not have any sort of listeners on resize or anything like that – MZimmerman6 Mar 03 '16 at 19:20
  • I've reproduced your code and after a manual resize the popup menu still works. The problem is elsewhere... – Xvolks Mar 03 '16 at 19:28
  • On which component have added the ClickListener ? Is it possible that this component does not behave as expected on resize event ? – Xvolks Mar 03 '16 at 19:31
  • @Xvolks it is added to the worldwind component. I do not want right click functionality anywhere else (at least at this moment) – MZimmerman6 Mar 03 '16 at 19:32
  • I'm not aware of this component, just check that it is not badly sized after the window resizing. – Xvolks Mar 03 '16 at 19:35
  • @xvolks, after reading a bit more about it apparently the WorldWind object I was using was a heavyweight canvas object while the jpopupmenu is a lightweight variant. The heavy object will always get drawn over the light one. I had to change some things about how i am instantiating the WW object, and it seems to be okay now. – MZimmerman6 Mar 03 '16 at 19:40
  • There might be a conflict due to WorldWind using JOGL interfaces. Perhaps you should ask in the WorldWind forum [link](http://forum.worldwindcentral.com/forumdisplay.php?37) – FredK Mar 03 '16 at 19:40
  • I didn't know that someone was still using heavyweight components . – Xvolks Mar 03 '16 at 19:46
  • @Xvolks Well WorldWind is released by NASA, a semi-government entity, so yeah, don't be surprised. – MZimmerman6 Mar 03 '16 at 19:49
  • Well, I stopped using heavyweight component since 1999 (with the beta version of swing for jdk 1.1.7), so I lost this kind of reflexion about the AWT technology. – Xvolks Mar 03 '16 at 19:52
  • @Xvolks Worldwind actually recommends using the heavyweight version since they state the JPanel version is unreliable on various systems, but it seems to cause other problems. It is a game of pick your poison. – MZimmerman6 Mar 03 '16 at 19:54
  • Strange game, good luck for your project ;) – Xvolks Mar 03 '16 at 19:56
  • @Xvolks Thanks, I would not be using Java if I had my choice :p – MZimmerman6 Mar 03 '16 at 19:56
  • Java it not so bad when you are used to. But the graphic interface systems (AWT, Swing, SWT, ...) are a mess. – Xvolks Mar 03 '16 at 19:58
  • Consider having a look at [`JComponent#setComponentPopupMenu`](https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setComponentPopupMenu(javax.swing.JPopupMenu))? – MadProgrammer Mar 03 '16 at 20:48
  • @Xvolks *"But the graphic interface systems (AWT, Swing, SWT, ...) are a mess"* as compared to any other framework with the same level for flexibility, configurability and personalisation, written at our about the same time? – MadProgrammer Mar 03 '16 at 20:50
  • @MadProgrammer we may have a serious discussion elsewhere on the mess level comparison of frameworks over the time ;) should be interesting... – Xvolks Mar 03 '16 at 21:05
  • @MadProgrammer I actually did end up finding that function as part of WorldWind a little bit ago. It works so much better. I didn't see it at first because honestly, the documentation for WorldWind is quite terrible – MZimmerman6 Mar 03 '16 at 22:39
  • @MZimmerman6 Part of the problem is the tutorials don't demonstrate it either :P – MadProgrammer Mar 03 '16 at 22:45
  • Glad to read that you managed to have it working ;) – Xvolks Mar 05 '16 at 16:16

0 Answers0