title: JButton disappears after requestFocusInWindow is called on the JPanel that contains the button
body: <p>I think my code will illustrate the problem, but basically I have a button in a Panel in a Panel in a Panel, and I want to get rid of that third Panel, but if I do I can't requestFocus anymore, because then the button disappears.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>import javax.swing.JFrame;
  
  public class A{
  
      private JFrame frame = new JFrame();    
      private C c = new C();
  
      public A() {
  
          //Now it works
          frame.add(c.getPanel());
          frame.pack();
          frame.setVisible(true);         
  
          //Now it won't
          //c.getPanel().requestFocusInWindow();      
      }
  
      public static void main(String[] args) {
          new A();
      }
  } 
  </code></pre>
  
  <p>and</p>
  
  <pre><code>import javax.swing.JButton;
  import javax.swing.JPanel;
  
  public class C {
  
      private JPanel mainpanel;
      private JButton button;
  
      public C(){
          button = new JButton("Back");
  
          //This fixes the issue, if you comment that same line in getPanel()
          //mainpanel = createPanel();
      }
  
      private JPanel createPanel() {
  
          JPanel panel = new JPanel();
          panel.add(button);
  
          return panel;   
      }
  
      public JPanel getPanel(){
          mainpanel = createPanel();
          return mainpanel;
          //I actually want it to be return createPanel(); getting rid of mainpanel altogether
      }
  }
  </code></pre>
  
  <p>This is just example code, I left out everything that isn't important.</p>
  
  <p>I want to call createPanel() in getPanel() to update the Panel before it's used, and I want the focus so I can navigate by Keyboard.</p>
  
  <p>What am I doing wrong?</p>