2013-03-15 77 views
0

我在JFrame中設置了這個方法,當用戶按下按鈕來「加載文件」時調用;但是,當用戶再次按下按鈕時。 。 。他們會在第一張桌子下面放置另一張桌子,這樣所有的信息都會被複制,並且超出我的「不可調整大小」的框架。TableModel,在Java JFrame中導致重複的數據庫信息

無論用戶按下按鈕多少次,我怎樣才能得到它只加載一個表?

JButton btnLoad = new JButton("Load File") 

private void GetAction() 
{ 
    ActionHandler handler = new ActionHandler(); 
    btnLoad.addActionListener(handler); 
} 

    private class ActionHandler implements ActionListener 
{  
    public void actionPerformed(ActionEvent evt) 
    { 
     String incmd = evt.getActionCommand(); 
     if (incmd.equals("Load File")) // If Load File button is pressed 
      DatabaseLoad(); 
     else if (incmd.equals("Exit")) // If Exit button is pressed 
      System.exit(0); 
    } 
} 
    private void DatabaseLoad() 
    { 
     try 
     { 
      // The driver allows you to query the database with Java 
      // forName dynamically loads the class for you   
      Class.forName("com.mysql.jdbc.Driver"); 

      // DriverManager is used to handle a set of JDBC drivers 
      // getConnection establishes a connection to the database 
      // You must also pass the userid and password for the database    
      String url = "jdbc:mysql://localhost:3306/charity"; 
      conn = DriverManager.getConnection (url,"root","password"); 

      // Statement objects executes a SQL query 
      // createStatement returns a Statement object    
      Statement sqlState = conn.createStatement(); 

      // This is the query I'm sending to the database 
      String selectStuff = "SELECT `name`, `charity`, `amount` FROM `charity`.`donations` ";     

      // A ResultSet contains a table of data representing the 
      // results of the query. It can not be changed and can 
      // only be read in one direction    
      rows = sqlState.executeQuery(selectStuff); 

      // Temporarily holds the row results    
      Object[] tempRow; 

      // next is used to iterate through the results of a query    
      while(rows.next()){ 

      // Gets the column values based on class type expected 
      tempRow = new Object[]{rows.getString(1), rows.getString(2), rows.getDouble(3) }; 

      // Adds the row of data to the end of the model    
      dTableModel.addRow(tempRow);     

     } 
      // Successfully loaded, message the user 
      message1.setText("<html><font color='red'>Database Info Loaded</font>"); 
      message2.setText(""); 
    } 
     catch (SQLException ex) 
     {    
      // String describing the error   
      System.out.println("SQLException: " + ex.getMessage()); 

      // Vendor specific error code    
      System.out.println("VendorError: " + ex.getErrorCode()); 
     } 
     catch (ClassNotFoundException e) 
     { 
      // Executes if the driver can't be found 
      System.out.println("Driver Cannot be found"); 
      e.printStackTrace(); 
     } 

     // Create a JTable using the custom DefaultTableModel  
     JTable table = new JTable(dTableModel); 

     // Increase the font size for the cells in the table   
     table.setFont(new Font("Serif", Font.PLAIN, 16)); 

     // Increase the size of the cells to allow for bigger fonts   
     table.setRowHeight(table.getRowHeight()+5); 

     // Allows the user to sort the data  
     table.setAutoCreateRowSorter(true); 

     // right justify amount column 
     TableColumn tc = table.getColumn("amount"); 
     RightTableCellRenderer rightRenderer = new RightTableCellRenderer(); 
     tc.setCellRenderer(rightRenderer);  

     // Disable auto resizing 
     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 

     // Set the width for the columns   
     TableColumn col1 = table.getColumnModel().getColumn(0); 
     col1.setPreferredWidth(200); 

     TableColumn col2 = table.getColumnModel().getColumn(1); 
     col2.setPreferredWidth(275); 

     TableColumn col3 = table.getColumnModel().getColumn(2); 
     col3.setPreferredWidth(75);  

     // Put the table in a scrollpane and add scrollpane to the frame   
     JScrollPane scrollPane = new JScrollPane(table); 
     scrollPane.setPreferredSize(new Dimension(552, 400)); 
     this.add(scrollPane, BorderLayout.CENTER); 
    } 

    // To change justification to the right 
    class RightTableCellRenderer extends DefaultTableCellRenderer { 
      public RightTableCellRenderer() { 
      setHorizontalAlignment(JLabel.RIGHT); 
     }   
    }  
+0

你可以發佈顯示你的'ActionListener'和'JButton'的代碼嗎? – 2013-03-15 16:39:56

+0

我已經添加了您詢問的部分,但它們非常基本,並且不認爲它們對錶 – 2013-03-15 17:19:17

回答

3

我怎樣才能得到它只能裝載一個表不管用戶多少次按下按鈕?

應該只在構建GUI組件時創建表和滾動窗格。然後,當你想改變數據,你只需改變現有表的模型:

table.setModel(updatedModel); 
+0

有任何影響,因此將jTable的東西移動到不同的方法中,然後根據是否希望顯示錶或不? – 2013-03-15 17:21:06

+0

按照我希望得到您的幫助的方式工作。 。 。謝謝 – 2013-03-15 18:29:29