Problems with SwingCanvas after RC2 update

Hello,
for my 3D-Tool I have integrated the 3D-Canvas in a SwingGUI. Before the update (before I used jme3Beta downloaded end of February) to RC2 the context shift between 3DCanvas and SwingGUI elements (for example Buttons) were not a problem. But after the update, if the 3DCanvas has the focus I need two clicks to get a reaction of the GUI-buttons. The following example consists of a 3DCanvas, a button and a panel. If I click the button the label text grow. If you compile the example with the RC2 version you need two clicks to get a reaction of the button.
Does anyone have a idea, how to fix this problem?

package jme3test.app;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * you can embed a jme canvas inside a swing application
 * @author pgi
 */
public class SwingCanvasTest extends SimpleApplication {
    static JLabel label = new JLabel("abc");

  @Override
  public void simpleInitApp() {
    // activate windowed input behaviour
    flyCam.setDragToRotate(true);
    // display a cube
    Box boxshape1 = new Box(new Vector3f(-3f,1.1f,0f), 1f,1f,1f);
    Geometry cube = new Geometry("My Textured Box", boxshape1);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
    mat.setColor("Color", ColorRGBA.Blue);
    cube.setMaterial(mat);
    rootNode.attachChild(cube);
  }

  public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
        // create new JME appsettings
        AppSettings settings = new AppSettings(true);
        settings.setWidth(640);
        settings.setHeight(480);
        settings.setFrameRate(50);

        // create new canvas application
        SwingCanvasTest canvasApplication = new SwingCanvasTest();
        canvasApplication.setSettings(settings);
        canvasApplication.createCanvas(); // create canvas!
        JmeCanvasContext ctx = (JmeCanvasContext) canvasApplication.getContext();
        ctx.setSystemListener(canvasApplication);
        Dimension dim = new Dimension(640, 480);
        ctx.getCanvas().setPreferredSize(dim);

        // Create Swing window
        JFrame window = new JFrame("Swing Application");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Fill Swing window with canvas and swing components
        JPanel panel = new JPanel(new FlowLayout()); // a panel
        panel.add(ctx.getCanvas());                  // add JME canvas
        //panel.add(new JButton("Swing Component"));      // add some Swing
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
        JButton button = new JButton("click me");
        button.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent ae) {
                  label.setText(label.getText()+"X");
              }
          });
        rightPanel.add(button);
        
        rightPanel.add(label);
        panel.add(rightPanel);
        window.add(panel);
        window.pack();

        // Display Swing window including JME canvas!
        window.setVisible(true);
        canvasApplication.startCanvas();
      }
    });
  }
}

Your code works fine on my computer (also RC 2)

Thank you for your reply.
I have tested a little bit more and I found out, that it depends on the operating system if the context shift between Swing and 3DCanvas works. I observed the problem on three different Linux systems, but with Windows the problem does not occur.
@benkibitzer: What OS did you use?

LWJGL is dodgy on Linux and Mac, especially with swing. We are still in the process of moving to JOGL to remedy some swing and Mac/Linux issues.

Thanks a lot for this information! How long does it take until JOGL is integrated in a stable version?

8 ulatrons.

1 Like

Hmm, and do you have a dirty hack to bypass the context shift problem?

Hmm, and do you have a dirty hack to bypass the context shift problem?

Idk, try to empirically determine whats happening on each platform and how you can counter it.