title: JFrame component must have a valid peer
body: <p>I'm trying to make my application go fullscreen whenever you press F11. To do this I create a new window and give it the same canvas used by the previous window.</p>
  <pre><code>public void setFullscreen(boolean bool) {
      fullscreen = bool;
      jf.dispose();
      jf = createFrame();
  }
  
  public JFrame createFrame() {
      JFrame jf = new JFrame(title);
      
      jf.getContentPane().setPreferredSize(new Dimension(width, height));
      jf.setMaximumSize(new Dimension(width, height));
      jf.setMinimumSize(new Dimension(width, height));
      jf.setLocationRelativeTo(null);
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      if(fullscreen) {
          jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
          jf.setUndecorated(true);
      }
      jf.setVisible(true);
      jf.setResizable(false);
      jf.add(canvas);
      jf.pack();
      jf.requestFocus();
      jf.setIconImage(img);
              
      return jf;
  
  }
  </code></pre>
  <p>If I render the window now, I randomly get the error &quot;java.lang.IllegalStateException: Component must have a valid peer&quot;. This is my render for my canvas.</p>
  <p>public void render() {</p>
  <pre><code>    BufferStrategy bs = this.getBufferStrategy();
      
      if(bs == null) {    
          this.createBufferStrategy(3);
          return;
      }
      
      Graphics g = bs.getDrawGraphics();
      
      g.setColor(color);
      g.fillRect(0, 0, 1920, 1080);
      
      //Render all the stuff on screen here
              
      g.dispose();
      bs.show();  
      
  }
  </code></pre>
  <p>To avoid this, I made my window only render its canvas when the frame is valid using the isValid() method.</p>
  <pre><code>public void render() {
      if(jf.isValid())
          canvas.render();
  }
  </code></pre>
  <p>This, however, does not solve it. Why do I still get the error?</p>
  <p>NOTE: I know that I am mixing awt and swing components (recently learned it's a bad idea), but switching from JFrame to a normal Frame doesn't solve it either.</p>