0

I have an LWJGL OpenGL Display showing up inside an AWT Canvas, which in turn is inside a Swing JPanel that is used as content pane for a Swing JFrame. At some point in the program, I want to switch the AWT Canvas containing the Display for a JComponent, so that instead of having something like that:

JFrame > JPanel > Canvas > Display

I have something like so:

JFrame > JPanel > JComponent

However, even though I remove the Canvas from the JPanel and add the JComponent, then revalidate the JPanel and repaint it, the Display still shows until I CTRL-ALT-SUPPR to task manager (my JFrame is set Undecorated and ExtendedState is JFrame.EXTENDED_BOTH, so it is full screen). At which point, the JComponent shows up like nothing ever happened..

I'll share the part of my code that does the transition so you can maybe help me point out what I have done wrong:

public static void switchTo(Container container){
    pan.removeAll();
    container.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
    pan.add(container);
    frame.getContentPane().validate();
    pan.revalidate();
    pan.repaint();
}

where pan is my JPanel and frame is my JFrame.

I have also tried directly setting my JComponent as my JFrame's content pane, but that gives the exact same result.

The only way I managed to make it function correctly was by calling destroy() on the Display beforehand; however, I need to keep the OpenGL context running so that I don't have to re-initialize the Display and reload every texture when switching back to the Display, which would be quite a long process given the number of textures I have.

Thank you very much for any answer, I hope I made myself clear!

Jon
  • 25
  • 1
  • 9
  • 1
    For better help, consider creating and posting a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve). We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem. You might very well solve the problem yourself by simply trying to isolate and expose the bug. – Hovercraft Full Of Eels Oct 19 '15 at 13:15
  • 1
    Also when you state, `"I need to keep the OpenGL context running so that I don't have to re-initialize the Display and reload every texture when switching back to the Display, which would be quite a long process given the number of textures I have."` -- this suggests that you may have a long-running bit of code that is being run on the Swing event thread, preventing this thread from doing its actions, including updating your display. If so, a possible solution is to run the long-running bit of code in a background thread. – Hovercraft Full Of Eels Oct 19 '15 at 13:17
  • Do *not* put heavyweight AWT components in lightweight Swing components. Rather than switching out the Canvas at runtime, avoid ever putting it in. You can almost certainly use a JPanel in its place. – John Bollinger Oct 19 '15 at 13:18
  • 1
    `pan.removeAll();` 1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Oct 19 '15 at 13:19
  • @John Bollinger The thing is, Display's setParent() method only takes in an AWT Canvas... – Jon Oct 19 '15 at 13:26
  • @Hovercraft Full Of Eels and Andrew Thompson Sorry about my example, I kind of thought that it would be clearer to just show the part of the code that seemed relevant to me, and quickly explain the rest. – Jon Oct 19 '15 at 13:26
  • @J0- re `"I kind of thought that it would be clearer to just show the part of the code that seemed relevant to me, and quickly explain the rest."` -- it's not. Regarding `"The thing is, Display's setParent() method only takes in an AWT Canvas"` -- what is the Display class and where is it from? – Hovercraft Full Of Eels Oct 19 '15 at 13:41
  • @J0- then it may be that you need to write an old-school AWT application to do what you want. I've no idea which `Display` class you are using, so I cannot give specific advice, but it is always a bad idea to put heavyweight components in lightweight containers. – John Bollinger Oct 19 '15 at 13:46
  • I'm talking about org.lwjgl.opengl.Display – Jon Oct 19 '15 at 15:15
  • @HovercraftFullOfEels – Jon Oct 19 '15 at 15:15

0 Answers0