Hello Guest

[BUG] Swing focus problems with LWJGL as Canvas

  • 0 Replies
  • 5237 Views
[BUG] Swing focus problems with LWJGL as Canvas
« on: December 30, 2015, 17:35:06 »
Hello

I've been trying to integerate the Canvas in a swing JFrame which seems to be working fine, but once I add other Swing components to that JFrame huge focus problems occur. Please use the following simple code to reproduce the problem easily (once more complex fields are added the focus problems get even worse very fast!):

Code: [Select]
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JFrame;
import javax.swing.JTextField;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class DesktopLauncher extends JFrame {
public static DesktopLauncher main;

private static final long serialVersionUID = 1L;

    private static int frameWidth = 800;
    private static int frameHeight = 600;
    private int displayWidth = 300;
    private int displayHeight = 300;

public static Canvas canvas;

    public static void main(String[] args) {
    DesktopLauncher launcher = new DesktopLauncher();
   
    try {
            Display.setDisplayMode(new DisplayMode(frameWidth, frameHeight));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
         
        // init OpenGL
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 800, 0, 600, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
       
        while (!Display.isCloseRequested()) {
            // Clear the screen and depth buffer
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); 
             
            // set the color of the quad (R,G,B,A)
            GL11.glColor3f(0.5f,0.5f,1.0f);
                 
            // draw quad
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glVertex2f(100,100);
            GL11.glVertex2f(100+200,100);
            GL11.glVertex2f(100+200,100+200);
            GL11.glVertex2f(100,100+200);
            GL11.glEnd();
     
            Display.update();
        }
         
        Display.destroy();
    }

public DesktopLauncher() {
addField();initGui();

        setSize(frameWidth, frameHeight);
        setDefaultCloseOperation(EXIT_ON_CLOSE);   
       
        setLayout(null);
        setVisible(true);
       
        this.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
            canvas.setBounds(0, 0, getWidth(), getHeight());     
            }
        });
}
   
private void addField() {
JTextField field = new JTextField();
field.setBounds(400, 300, 200, 200);
add(field);

field.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
System.out.println("field got focus!");
}

@Override
public void focusLost(FocusEvent e) {
System.out.println("field lost focus!");
}
});
}

private void initGui() {
        canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(displayWidth, displayHeight));
        canvas.setIgnoreRepaint(true);
        canvas.setBounds(0, 0, frameWidth, frameHeight);
       
        try {
            Display.setParent(canvas);
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
       
        add(canvas);
}
}

I am using the following:

- LWJGL 2.9.3
- JRE 8 (jre1.8.0_65)
- Windows 10

It would be really nice for me if this can get fixed!