LWJGL Forum

Programming => General Java Game Development => Topic started by: bartvbl on November 19, 2011, 01:48:52

Title: Display.create() fails when I try to use it in combination with setParent()
Post by: bartvbl on November 19, 2011, 01:48:52
So I have made a little main menu in swing, and when starting the game, I would like the same window to hold a Canvas instance, used as parent for LWJGL.
Here is my code for setting up the window:

JFrame frame = new JFrame("Puzzler");
Canvas canvas = new Canvas();
canvas.addComponentListener(adapter);
canvas.setIgnoreRepaint(true);
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);


Next, I create the JPanel class that draws and handles the main menu:

this.mainMenuPanel = new MainMenuPanel(this.main);
this.jframe.setSize(500, 170);
this.jframe.getContentPane().add(mainMenuPanel);
this.jframe.validate();


This all works. Main menu shows up nicely, behaves as it should.
When it is finished, it calls a final function to add the canvas to the main jframe:

JPanel container = new JPanel(new BorderLayout());
container.add(canvas, BorderLayout.CENTER);
this.jframe.setVisible(false);
this.jframe.removeAll();
this.jframe.getContentPane().add(container);
this.mainMenuPanel = null; //mark for garbage collection
this.jframe.setSize(640, 480);
this.jframe.setVisible(true);
this.jframe.validate();


There is something in this function that causes the error when I finally call Display.create(); with the canvas as parent:

org.lwjgl.LWJGLException: Parent.isDisplayable() must be true

Can anyone help me further? Thanks in advance :)
Title: Re: Display.create() fails when I try to use it in combination with setParent()
Post by: kappa on November 19, 2011, 12:53:08
It looks like your trying to create the Display before the Canvas is even visible. Look at the basic applet example (http://www.lwjgl.org/wiki/index.php?title=Basic_LWJGL_Applet) to see how the problem is fixed using addNotify/removeNotify.
Title: Re: Display.create() fails when I try to use it in combination with setParent()
Post by: bartvbl on November 19, 2011, 15:13:32
I looked at that, but it didn't get me any further; in both cases I get a screen that only contains the original sized viewPort, running the game perfectly. However, the screen does not handle resize updates. Neither does it want to close.
I have moved the main loop in yet another thread, with the hypothesis that java's event handling got stuck in my main loop, but this did not fix the problem; it resulted in the exact same problem.
Basically, I can get a responding JFrame containing a Canvas that responds to resizing events.
Until I create the display. It just hangs after that.

Edit
Let me just restate the question in another way; has anyone here used some swing components to launch LWJGL using the same window (as in; a similar way as I have)? Or does there exist code where the two are used in conjunction with each other?

Edit (2)
I solved the problem. As it turned out I was calling run() on my main thread from the java awt event thread instead of start(), causing the java awt thread to execute my main loop. It works flawlessly now. Silly, silly, me.