How to attach opengl display to a JFrame and dispose of it properly?

+4 Timmy Cafe · November 13, 2014
i am new to this website and i am needing some help. How can i attach the OpenGl display to a JFrame and so that when i close the JFrame is destroys the display? Here is my code so far:

package test.core;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

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

import static org.lwjgl.opengl.GL11.*;

public class Main {

   private static CreateCanvas canvas;
   private static CreateFrame frame;

   private static int width = 800;
   private static int height = 600;

   public static void main(String[] args) throws InterruptedException {



   public static void cleanUp() {

   private static void startDisplay() {
       }catch(LWJGLException ex)
           Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
   private static void startFrames()
       Runnable r = new Runnable(){
           public void run(){
               frame = new CreateFrame();
               JButton button = new JButton("BUTTON");
               canvas = new CreateCanvas();
               JPanel panel = frame.panel;


               canvas.setSize(300, 300);
               frame.setSize(width, height);


               WindowListener listen = new WindowAdapter(){
                   public void windowClosing(WindowEvent we){
                       int result = JOptionPane.showConfirmDialog(frame, "Do you want to quit the Application?");
                       if(result == JOptionPane.OK_OPTION){





I had the opengl display attach to the JFrame before i did the runnable. But after adding the runnable the display now shows up the same size as my screen size. I have tried rearranging the


and the


but nothing changes the opengl display is still the same size and when i try to close the JFrame first rather then close the display first i get this error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: From thread Thread[AWT-EventQueue-0,6,main]: Thread[main,5,main] already has the context current

which points me to my


which im guessing is telling me that i am not properly disposing the display? Can anyone help me attach the opengl display to the JFrame and fix the error above?

Post a Reply


Oldest  Newest  Rating
0 Timmy Cafe · November 21, 2014
I think i have tried everything i know and i still just can not figure it out! been trying to figure this out for a month and still nothing.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator