简体   繁体   中英

JMenuItem desplaying behind GLCanvas?

I have some problems with the JMenuItem that is displayed behind the GLCanvas.

At first time when clicking on the JMenuItem it shows in front of the GLCanvas, but once clicking in the JSplit to stretch it I get this problem.

I found that this problem is caused generally when using Swing components with AWT components,

Have you any idea how to solve this issue.

在此处输入图片说明

This is my code.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.jogamp.opengl.awt.GLCanvas;

public class MenuTest extends JFrame {
      public MenuTest()      {
            this.addWindowListener      (new WindowAdapter(){
                  public void windowClosing(WindowEvent e){
                        dispose();
                        System.exit(0);
                  }
            });
      }

      public static void main(String args[]) {
        int numberOfMenuItems = 5;
            MenuTest f = new MenuTest();
            Container cont = f.getContentPane();
            cont.setLayout(new GridLayout(2,1));
            JMenuBar menuBar = new JMenuBar();
            JMenu menu = new JMenu("testMenu");
            for(int i=0; i<numberOfMenuItems; i++){
                JMenu item = new JMenu("Item "+i);
                item.add(new JMenuItem("item"));
                menu.add(item);

            }
            menuBar.add(menu);
            f.setJMenuBar(menuBar);


            GLCanvas canvas = new GLCanvas();
            canvas.setBackground(new Color(100, 50, 100));
            JSplitPane split = new JSplitPane();

            Container wrappingContainer = new Container();
            wrappingContainer.setLayout(new BorderLayout());
            wrappingContainer.setBackground(new Color(100,100,100));
            wrappingContainer.add(canvas);

            split.add(wrappingContainer, JSplitPane.RIGHT);
            split.add(new JPanel(), JSplitPane.LEFT);

            f
            .getContentPane().add(split);

            f.setSize(400,200);
            f.show();
      }
}

JPopupMenu has a method:

setDefaultLightWeightPopupEnabled(false);

and JMenu has a method:

getPopupMenu();

So I would guess you need to iterate through all your menus to get the popup menu and make it a heavy weight popup.

See Mixing Heavyweight and Lightweight Components for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM