PopupMenu on GlCanvas

Hi, I am trying to get a popup menu to appear over a glcanvas when the user hits the right mouse button.

I have tried the JPopupmenu but this does not appear over the glcanvas even after setting :

JPopupMenu.setDefaultLightWeightPopupEnabled(false);

Have also tried the awt PopupMenu but every time I click on the glcanvas I get a null pointer exception telling me that the parent (the glcanvas?) is null (and yet it can’t be null else I wouldn’t be able to use it for the rest of the application would I). I have enclosed the code for where the null pointer exception occurs below (it is line 6 which does it :o )

// gives null pointer exception
public void mousePressed(MouseEvent e)
{
  if (  e.isPopupTrigger() )       
  {
    gui.popupMenu.show(e.getComponent(), e.getX(), e.getY());  // line 6
  }
}

Any tips/code (preferably) or advice on getting a popupmenu to appear at the point where a user right clicks on a glcanvas would be greatly appreciated.

Many thanks

Sally

I’ve dealt with null pointer things before, and it’s always been something stupid that I forgot to do (or I wasn’t synchronizing the methods which used/created/deleted variables), so forgive me for asking a potentially stupid question.

Is the popup object existent at the time of use? I mean, have you previously created it, absolutely, no questions, it exists by the time you’re ABLE to click?

I’m not sure what might be causing the problem beyond that, however, without some more source code. Maybe, can you create a super small app that demonstrates this faulty functionality and I’ll take a look at it for you?

(I don’t guarentee anything, but I’d be happy to look at it. I’m not an expert programmer, but I’ve gotten really good at debugging through my experiences! ^_^)

Hi, many thanks for your reply. A super small app which demonstrates the problem is below. Right clicking on the canvas will display 3 cartoon character names. Don’t know whether the bug(feature?) would be the same on another os, because I’ve only tried it on linux. Please let me know. The right click menu only seems to come up when I right click near the bottom edge of the canvas. Any help appreciated.

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import net.java.games.jogl.*; 
 
public class PopupTest extends JFrame implements MouseListener, GLEventListener 
{ 
GLCanvas canvas = null; 
JPopupMenu popupMenu = new JPopupMenu("my menu"); 
public static final Dimension PREFERRED_FRAME_SIZE = new Dimension (400, 400); 
  public PopupTest() 
  { 
    JPopupMenu.setDefaultLightWeightPopupEnabled(false); 
    GLCapabilities capabilities = new GLCapabilities(); 
    canvas = GLDrawableFactory.getFactory().createGLCanvas(capabilities); 
    this.getContentPane().add (canvas, BorderLayout.CENTER); 
    this.getContentPane().add(canvas); 
    canvas.addGLEventListener(this); 
    canvas.addMouseListener(this); 
    // add test items to the popup menu 
    popupMenu.add(new JMenuItem("Micky Mouse")); 
    popupMenu.add(new JMenuItem("Donald Duck")); 
    popupMenu.add(new JMenuItem("Scooby Doo")); 
    //needed to make the menus display over GLCanvas 
 
  } 
 
  public Dimension getPreferredSize () 
  { 
    return PREFERRED_FRAME_SIZE; 
  } 
 
  public static void main(String[] args) 
  { 
    PopupTest put = new PopupTest(); 
    put.pack(); 
    put.setVisible(true); 
  } 
 
  public void mouseExited(MouseEvent me) {} 
  public void mouseEntered(MouseEvent me) {} 
  public void mouseClicked(MouseEvent me) {} 
 
  public void mouseReleased(MouseEvent me) 
  { 
    if (me.isPopupTrigger() ) 
    { 
 popupMenu.show(me.getComponent(), me.getX(), me.getY()); 
    } 
  } 
 
  public void mousePressed(MouseEvent me) 
  { 
    if (me.isPopupTrigger()) 
    { 
 popupMenu.show(me.getComponent(), me.getX(), me.getY()); 
    } 
  } 
 
  public void displayChanged (GLDrawable drawable,boolean mc, boolean dc){} 
 
  public void init (GLDrawable drawable) 
  { 
    GL gl = drawable.getGL(); 
    gl.glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); //white 
    gl.glClear (GL.GL_COLOR_BUFFER_BIT); 
  } 
 
  public void reshape (GLDrawable drawable, int x, int y, int width, int height) 
  {} 
  public void display (GLDrawable drawable) 
  {} 
} 

The problem in your example is order of initialization. The popupMenu variable is initialized before the PopupTest constructor is entered. Therefore the call to JPopupMenu.setDefaultLightWeightPopupEnabled(false); has no effect for that JPopupMenu. You can either call popupMenu.setLightWeightPopupEnabled(false); or move the initialization of the popupMenu variable into the PopupTest constructor.

Thanks very much for that. You’re absolutely right. Glad that’s working now. :slight_smile: