title: How to add external JPanel in JFrame?
body: <p><img src="https://i.stack.imgur.com/IMWJu.png" alt="enter image description here"></p>
  
  <p>I'm working on large scale program. As you can see I have one main JFrame and about 20 menu items on that. Each menu item must pop up a new window. At the beginning I have created a JLayeredPanel and then I assigned each menu item to one JPanel which is inside JFrame.Then I put 25 panel in JLayeredPanel... Default all the panels are set to invisible like:</p>
  
  <pre><code>panel1.setVisible(false);
  panel2.setVisible(false);
  </code></pre>
  
  <p>so on</p>
  
  <p>When user click on one menu item, its JPanel will be visible and rest are invisible. It looks messy and I have 5000 lines code. I used InternalFrame and TabbedPane but I'm not happy with them. I want to split my code in different JPanel classes and assign them to the main JFrame. I mean when user clicked on each menu item it will call the external JPanel and render it on the JPanel on the main JFrame. I am using design mode in netbeans and it does everything for me but the simpled structure is like this and it is not working:</p>
  
  <pre><code> public class NewJPanel extends JPanel{
      //I have added buttons and etc on this panel
      ......
  
      }
  
  public class  frame extends JFrame(){
  
          JPanel panel = new JPanel();
          .....
          Public frame(){
               frame.add(panel);
          }
          ......
          //When use click on the any button on the panel
          private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
              //this is not working
  
              NewJPanel fi = new NewJPanel ();
              panel1.add(fi);
  
              //or I tested this way separately but it did not work
  
             panel1.remove();
             panel1 = new NewJPanel();
             add(panel);
             invalidate();
          }       
  
  
  }
  </code></pre>
  
  <p>please give me any suggestion how I can control this program in splited classes in professional way. </p>