2016-03-03 46 views
0

当我点击名为“注册主题”的按钮时,会显示一个显示注册主题的表格。数据从文本文件加载。但是,当我转到应用程序的另一个页面并返回到“注册的主题”,或者再次单击“注册的主题”按钮时,新表格将加载并添加到先前加载的表格中。这使得每次单击“注册的主题”时,表格都会变得更长。我已经添加了一个removeAll()方法,它在再次加载表之前清除窗格的内容,但由于某种原因,之前加载的表格不会消失。表的上一个实例附加到新调用的表。该页面应该只显示一个表格

下面是表格所在面板的代码。

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); 
     } 
    } 

创建表

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 <= 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 :)"); 
      }  
     } 
    } 

,其中面板连接到

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); 
    } 
} 

在此先感谢主类的类!

回答

1

在向表中添加行之前,请尝试在LoadTable类中使用以下代码。它将清除表模型中的行。

dtm.setRowCount(0); 

因此新LoadTable类应该是这样的:

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); 
    dtm.setRowCount(0); //THIS WILL CLEAR THE ROWS IN YOUR STATIC TABLEMODEL 
    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 <= 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 :)"); 
     }  
    } 
} 
+0

噢,我的。那样做了!非常感谢。我不知道修复是如此简单。 –

相关问题