Problem mixing GLCanvas (heavyweight) and Swing

Hello,

I’m trying to build a reusable OpenGL widget for Swing using a JPanel and a GLCanvas (to avoid perf of GLJPanel)

I have a problem to display the Swing popup menu on the surface display of the GLCanvas.

To illustrate this problem, I wrote a small test program:


import java.awt.BorderLayout;
 
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
 
public class GLW extends JPanel{
	private static final long serialVersionUID = 1L;
	private GLWCanvas canvas;
	private JPopupMenu popupmenu;
	private JMenuItem jMenuItem0;
	
	public GLW() {
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		this.setLayout(new BorderLayout());
		this.setComponentPopupMenu(getJPopupMenu0());
		this.add(getMixTestCanvas()); // ajout du widget hérité de GLCanvas
	}
	
	private GLWCanvas getMixTestCanvas(){
		if (canvas == null) {
			canvas = new GLWCanvas();
			canvas.addGLEventListener(canvas);
		}
		return canvas;
	}
	
	private JPopupMenu getJPopupMenu0() {
		if (popupmenu == null) {
			popupmenu = new JPopupMenu();
			popupmenu.add(getJMenuItem0());
		}
		return popupmenu;
	}
	
	private JMenuItem getJMenuItem0() {
		if (jMenuItem0 == null) {
			jMenuItem0 = new JMenuItem();
			jMenuItem0.setText("jMenuItem0");
 
		}
		return jMenuItem0;
	}
}

In this code the GLWCanvas is a class inherited from GLCanvas.

When I commented the line

this.add (getMixTestCanvas ());

The widget inherited from the widget GLCanvas is not displayed and the popup menu works fine. So far, everything is normal.

As against when I uncomment this line, the widget inherited from the widget GLCanvas is displayed (again, everything is normal), but the menu is not displayed (and here, it may be normal regarding my code, but it is not what I want!).

I tried to disable the lightweight nature of the JPanel with

JPopupMenu.setDefaultLightWeightPopupEnabled (false);

but it seems no effect. Does anyone have an idea for me to reach my purpose?

Thank you in advance.

I’m pretty sure you have to disable light weight pop-ups before you create any Swing objects, so it shouldn’t be done in your constructor for GLW. Give that a shot and hopefully it works.

Yep - try it in your main() method or in a static initializer block.

Thanks for your responses.

Unfortunately, it doesn’t work better. I put the code


JPopupMenu.setDefaultLightWeightPopupEnabled(false);

at the first line of the main method, but no change can be noticed.

Here is my main class:


import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPopupMenu;

public class MixTest {
	public static void main(String[] args) {
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);

		JFrame frame = new JFrame();
		GLW glw = new GLW();
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("MixTest");
		frame.getContentPane().setPreferredSize(new Dimension(300, 200));
		frame.add(glw);
		frame.pack();
		frame.setVisible(true);
	}
}

The GLW class is the same excepts the constructor is now :


        public GLW() {
		this.setLayout(new BorderLayout());
		this.setComponentPopupMenu(getJPopupMenu0());
		this.add(getMixTestCanvas());
        }

I can’t figure out where the problem is coming from… So I hope somebody could help me. Thanks.

jMax

What system are you using?
Do you use some non-default look and feel?

Could you provide a self-contained compilable test case as zip?

I’m using Debian Lenny.
=> uname -a returns:


Linux  2.6.26-1-686 #1 SMP Fri Mar 13 18:08:45 UTC 2009 i686 GNU/Linux

JDK is Sun’s one.
=> dpkg -l | grep sun returns:


ii  sun-java6-bin                                6-12-1                        Sun Java(TM) Runtime Environment (JRE) 6 (architecture depende
ii  sun-java6-jdk                                6-12-1                        Sun Java(TM) Development Kit (JDK) 6
ii  sun-java6-jre                                6-12-1                        Sun Java(TM) Runtime Environment (JRE) 6 (architecture indepen

Default Swing Look and Feel is Nimbus .
=> cat swing.properties returns:


swing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel

I tried to disable default swing LookAndFeel by temporarily renaming the file swing.properties, but once again no change can be noticed.

My video card is ATI FireGL 8800 and my video driver is open source radeon.

I hope this may help you to help me :wink:

If problem is related to system and/or swing look and feel, I may try my test code against others platform. Actually, I already tried against another lenny machine (with intel i965 video card), but result is still bad.

Anyway, thanks for your response.

jMax

I solved the problem adding


this.popupmenu.show(canvas, mouseEvent.getX(), mouseEvent.getY());

in the right listener…

in this code this is a JPanel derived class object and canvas is a GLCanvas derived class object.

The line


JPopupMenu.setDefaultLightWeightPopupEnabled(false);

seems now having no effect.

Hope this may help…