title: Previous instance of table appends to the newly invoked table. The page should show only one table
body: <p>When I click the button called Enrolled Subjects, a table showing enrolled subjects is invoked. The data is loaded from a text file. But when I go to another page of the application and go back to the Enrolled Subjects or when I click the Enrolled Subjects Button again, a new table loads and adds to the previously loaded table. This makes the table grow longer every time I click Enrolled Subjects. I have added a removeAll() method which clears the content of the pane before loading the table again, but the previously loaded table, for some reason, does not go away. </p>
  
  <p>Here's the code of the panel where the table is attached. </p>
  
  <pre><code>  public class EnrolledSubjectsPanel extends JPanel
      {
          public EnrolledSubjectsPanel(String path)
          {
  
              setBackground(Color.LIGHT_GRAY);
              setBounds(183, 280, 1129, 401);
              setLayout(null);
  
              LoadTable table = new LoadTable(path);
              table.setBounds(0,60,1129,600);
              add(table);
  
              JLabel lblEnrolledSubjects = new JLabel("Enrolled Subjects");
              lblEnrolledSubjects.setFont(new Font("Tahoma", Font.PLAIN, 35));
              lblEnrolledSubjects.setBounds(446, 0, 292, 43);
              add(lblEnrolledSubjects);
              setVisible(true);
          }
      }
  </code></pre>
  
  <p>The class that creates the table</p>
  
  <pre><code>    public class LoadTable extends JPanel
      {
          private static String path;
          private static  String header[] = {"Subject", "Schedule", "Room", "Proferssor", "Units"};
          private static  DefaultTableModel dtm = new DefaultTableModel(null, header);
          boolean isLaunched;
          public LoadTable(String path)
          {
              this.path = "subjects" + path;
  
              setBackground(Color.LIGHT_GRAY);
              setBounds(183, 280, 1129, 401);
              setLayout(null);
  
              JTable table = new JTable(dtm);
              JScrollPane jsp = new JScrollPane(table);
              jsp.setBounds(0, 0, 1129, 380); 
              add(jsp);
  
              try
              {
              TextReaderBasic file = new TextReaderBasic(path);
              String data[] = file.openFile();
              int i = 0;
              for(i = 0; i &lt;= data.length; i++)
              {
  
                  addRow(i);
              }
  
              }
              catch(Exception e)
              {
                  System.out.println(e.getMessage());
              }
              setVisible(true);
          }
  
          public static void addRow(int x)
          {   
              try
              {       
              TextReaderBasic file = new TextReaderBasic(path);
              String data[] = file.openFile();
              int size = data.length;
              int i = 0 + x;
              int j = 6 + x;
              int k = 12 + x;
              int l = 18 + x;
              int m = 24 + x;
  
              dtm.addRow(new Object[]{data[i], data[j], data[k], data[l], data[m]});
              }
              catch(Exception e)
              {
                  System.out.println("Action completed :)");
              }       
          }
      }
  </code></pre>
  
  <p>The main class where the panel is attached to</p>
  
  <pre><code>public class LaunchPage extends JFrame
  {   public LaunchPage(String path) 
      {
          getContentPane().setBackground(Color.white);
          getContentPane().setLayout(null);
  
          StudentBasicInformationPanel studentBasicInfo = new StudentBasicInformationPanel(path);
          getContentPane().add(studentBasicInfo);
  
          JLabel universityTitleL = new JLabel("Evil Genuises University");
          universityTitleL.setFont(new Font("Edwardian Script ITC", Font.ITALIC, 42));
          universityTitleL.setBounds(514, 11, 343, 65);
          getContentPane().add(universityTitleL);
  
          JPanel panelToAttach = new JPanel();
          panelToAttach.setBounds(173, 280, 1129, 404);
          getContentPane().add(panelToAttach);
          setSize(1366, 768);
  
          JButton enrollmentButton = new JButton("Enrollment");
          enrollmentButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) 
              {
              panelToAttach.removeAll();
  
              EnrollmentPanel ep = new EnrollmentPanel();
              ep.setBackground(Color.LIGHT_GRAY);
              ep.setBounds(0, 0, 1129, 401);
              panelToAttach.add(ep);
              repaint();
              }
          });
          enrollmentButton.setBounds(10, 280, 157, 58);
          getContentPane().add(enrollmentButton);
  
          JButton viewEnrolledSubjectsButton = new JButton("Enrolled Subjects");
          viewEnrolledSubjectsButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) 
              {
                  panelToAttach.removeAll();
                  EnrolledSubjectsPanel es = new EnrolledSubjectsPanel(path);
                  es.setBackground(Color.LIGHT_GRAY);
                  es.setBounds(0, 0, 1129, 401);
                  panelToAttach.add(es);  
                  repaint();
              }
          });
          viewEnrolledSubjectsButton.setBounds(10, 349, 157, 58);
          getContentPane().add(viewEnrolledSubjectsButton);
  
          JButton viewGradesButton = new JButton("View Grades");
          viewGradesButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0)
              {
                  panelToAttach.removeAll();
                  GradesPanel gp = new GradesPanel();
                  gp.setBackground(Color.LIGHT_GRAY);
                  gp.setBounds(0, 0, 1129, 401);
                  panelToAttach.add(gp);
                  repaint();
              }
          });
          viewGradesButton.setBounds(10, 418, 157, 58);
          getContentPane().add(viewGradesButton);
  
          JButton clearanceButton = new JButton("Clearance");
          clearanceButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) 
              {
                  panelToAttach.removeAll();
                  ClearancePanel cp = new ClearancePanel();
                  cp.setBackground(Color.LIGHT_GRAY);
                  cp.setBounds(0, 0, 1129, 401);
                  panelToAttach.add(cp);
                  repaint();
              }
          });
          clearanceButton.setBounds(10, 487, 157, 58);
          getContentPane().add(clearanceButton);
  
          JButton viewAccountButton = new JButton("View Account");
          viewAccountButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) 
              {
                  panelToAttach.removeAll();
                  AccountPanel ap = new AccountPanel();
                  ap.setBackground(Color.LIGHT_GRAY);
                  ap.setBounds(0, 0, 1129, 401);
                  panelToAttach.add(ap);
                  repaint();
              }
          });
          viewAccountButton.setBounds(10, 556, 157, 58);
          getContentPane().add(viewAccountButton);
  
          JButton closeButton = new JButton("Close");
          closeButton.addActionListener(new ActionListener() 
          {
              public void actionPerformed(ActionEvent e) 
              {
                  System.exit(0);
              }
          });
  
          closeButton.setBounds(10, 626, 157, 58);
          getContentPane().add(closeButton);
  
          setVisible(true);
      }
  }
  </code></pre>
  
  <p>Thanks in advance!</p>