2013-03-02 70 views
0

我有下面的代碼:組合框模型setselecteditem

package nypdapp; 

import java.sql.*; 
import javax.swing.*; 

public class CaseFileAddFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form CaseFileFrame 
    */ 
    private Connection Econn; 
    private int cfid; 
    private boolean wijzig; 
    private DefaultListModel DetectiveListModel = new DefaultListModel(); 
    private DefaultListModel EmployeeTotListModel = new DefaultListModel(); 
    private DefaultListModel WitnessModel = new DefaultListModel(); 
    private DefaultListModel EvidenceModel = new DefaultListModel(); 
    private DefaultListModel SuspectsModel = new DefaultListModel(); 
    private DefaultComboBoxModel DistrictModel = new DefaultComboBoxModel(); 
    private DefaultComboBoxModel LocationModel = new DefaultComboBoxModel(); 
    SpinnerModel spmodel = new SpinnerNumberModel(2013,1950,2013,1); 
    public CaseFileAddFrame(int cfid, boolean wijzig) { 
     initComponents(); 
     this.cfid = cfid; 
     this.wijzig=wijzig; 
     s1.setVisible(false); 
     s3.setVisible(false); 
     s4.setVisible(false); 
     YearSn.setModel(spmodel); 
     try 
     { 
      SimpleDataSource.init(); 
      Econn = SimpleDataSource.getConnection(); 
     } 
     catch(SQLException | ClassNotFoundException e) 
     { 
      System.out.println(e); 
     } 
     setDistricts(); 
     getSelectedDistrict(); 
     ClassCB.setEnabled(false); 
     if(wijzig == true) 
     { 
      setInformation(); 
     }   
    } 

    private void setLocation(int lid) 
    { 
     try 
     { 
      Statement userStat = Econn.createStatement(); 
      String userquery = "SELECT name, district_name FROM location l INNER JOIN district d ON l.distr_ic = d.district_id WHERE l_id = "+lid; 
      ResultSet result = userStat.executeQuery(userquery); 
      result.next(); 
      String name = result.getString("district_name"); 
      System.out.println("DBName: "+name); 
      int i = DistrictModel.getSize(); 
      for(int p =0; p < i; p++) 
      { 
       System.out.println("Current:"+DistrictModel.getElementAt(p)); 
       if(DistrictModel.getElementAt(p).equals(name)) 
       { 
        //DistrictModel.setSelectedItem(p); 
        DistrictCB.setSelectedIndex(p); 
        System.out.println("Done<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); 
       } 
      } 
     } 
     catch(SQLException e) 
     { 
      System.out.println(e); 
     }   
    } 
    private void setInformation() 
    { 
     try 
     { 
      Statement userStat = Econn.createStatement(); 
      String userquery = "SELECT * FROM casefile WHERE cf_id = "+this.cfid; 
      ResultSet result = userStat.executeQuery(userquery); 
      result.next(); 
      switch(result.getInt("crime")) 
      { 
       case 0: 
       { 
        CrimeCB.setSelectedIndex(0); 
       } 
        break; 
       case 1: 
       { 
        CrimeCB.setSelectedIndex(1); 
       } 
        break; 
       case 2: 
       { 
        CrimeCB.setSelectedIndex(2); 
       } 
        break; 
       default: 
       { 
        CrimeCB.setSelectedIndex(0); 
       } 
      } 
      if(CrimeCB.getSelectedIndex() == 2) 
      { 
       ClassCB.setEnabled(true); 
      switch (result.getInt("classtype")) 
      { 
       case 0: 
       { 
        ClassCB.setSelectedIndex(0); 
       } 
        break; 
       case 1: 
       { 
        ClassCB.setSelectedIndex(1); 
       } 
        break; 
       case 2: 
       { 
        ClassCB.setSelectedIndex(2); 
       } 
        break; 
       case 3: 
       { 
        ClassCB.setSelectedIndex(3); 
       } 
        break; 
       case 4: 
       { 
        ClassCB.setSelectedIndex(4); 
       } 
        break; 
       case 5: 
       { 
        ClassCB.setSelectedIndex(5); 
       } 
        break; 
       case 6: 
       { 
        ClassCB.setSelectedIndex(6); 
       } 
        break; 
       case 7: 
       { 
        ClassCB.setSelectedIndex(7); 
       } 
        break; 
       case 8: 
       { 
        ClassCB.setSelectedIndex(8); 
       } 
        break; 
       default: 
       { 
        ClassCB.setSelectedIndex(0); 
       } 
      } 
      } 
      else 
      { 
       ClassCB.setEnabled(false); 
      } 

      switch (result.getInt("type")) 
      { 
       case 0: 
       { 
        TypeCB.setSelectedIndex(0); 
       } 
        break; 
       case 1: 
       { 
        TypeCB.setSelectedIndex(1); 
       } 
        break; 
       case 2: 
       { 
        TypeCB.setSelectedIndex(2); 
       } 
        break; 
       case 3: 
       { 
        TypeCB.setSelectedIndex(3); 
       } 
        break; 
       case 4: 
       { 
        TypeCB.setSelectedIndex(4); 
       } 
        break; 
       case 5: 
       { 
        TypeCB.setSelectedIndex(5); 
       } 
        break; 
       case 6: 
       { 
        TypeCB.setSelectedIndex(6); 
       } 
        break; 
       case 7: 
       { 
        TypeCB.setSelectedIndex(7); 
       } 
        break; 
       case 8: 
       { 
        TypeCB.setSelectedIndex(8); 
       }  
        break; 
       default: 
       { 
        TypeCB.setSelectedIndex(0); 
       } 
      } 
      SubjectTF.setText(result.getString("subject")); 
      Date date = result.getDate("date"); 
      int month = date.getMonth(); 
      int day = date.getDate(); 
      int year = date.getYear()+1900; 
      MonthCB.setSelectedIndex(month); 
      DayCB.setSelectedIndex(day); 
      spmodel.setValue(year); 
      AssignedDATF.setText(result.getString("assignedDA")); 
      FRNameTF.setText(result.getString("fr_id")); 
      DescriptionTA.setText(result.getString("description")); 
      setLocation(result.getInt("lid")); 
     } 
     catch(SQLException e) 
     { 
      System.out.println(e); 
     }  
    }  
    private void setDistricts() 
    { 
     try 
     { 
      Statement userStat = Econn.createStatement(); 
      String userquery = "SELECT district_id, district_name FROM district"; 
      ResultSet userResult = userStat.executeQuery(userquery); 
      while(userResult.next()) 
      { 
       int ids = userResult.getInt("district_id"); 
       String name = userResult.getString("district_name"); 
       DistrictModel.addElement(new DistrictItem(name,ids)); 
      } 
      DistrictCB.setModel(DistrictModel); 
     } 
     catch(SQLException e) 
     { 
      System.out.println(e); 
     } 
    }            

    private void SubmitCaseFileButtonActionPerformed(java.awt.event.ActionEvent evt) {              
     if(validation() == false) 
     { 
      javax.swing.JOptionPane.showMessageDialog(null, "WARNING\nCheck the field validation!\nIf there is no Assigned DA Enter 'Unknown'"); 
     } 
     else if(wijzig == false) 
     { 
      int crime = CrimeCB.getSelectedIndex(); 
      int crimeclass; 
      if(ClassCB.isEnabled() == true) 
      { 
       crimeclass = ClassCB.getSelectedIndex(); 
      } 
      else 
      { 
       crimeclass = 0; 
      } 
      String subject = SubjectTF.getText(); 
      int month = MonthCB.getSelectedIndex()+1; 
      int day = DayCB.getSelectedIndex()+1; 
      int year = Integer.parseInt(spmodel.getValue().toString()); 
      Date date = ToSQLClass.toSqlDate(year, month, day); 
      int crimetype = TypeCB.getSelectedIndex(); 
      LocationSItem li = (LocationSItem)LocationCB.getSelectedItem(); 
      int locationid = li.id; 
      String assignedDA = AssignedDATF.getText(); 
      String description = DescriptionTA.getText(); 
      String fr = FRNameTF.getText(); 
      try 
      { 
       PreparedStatement addstat = Econn.prepareStatement("INSERT INTO casefile VALUES(cf_id,?,?,?,?,?,?,?,?,?,?)"); 
       addstat.setString(1, subject); 
       addstat.setInt(2, crime); 
       addstat.setInt(3, crimetype); 
       addstat.setString(4, description); 
       addstat.setString(5, assignedDA); 
       addstat.setString(6, fr); 
       addstat.setNull(7,java.sql.Types.INTEGER); 
       addstat.setDate(8, date); 
       addstat.setInt(9, crimeclass); 
       addstat.setInt(10, locationid); 
       addstat.execute();  
       javax.swing.JOptionPane.showMessageDialog(null, "WARNING\nCasefile added!\n\nDon't forget to add suspects, witnesses, detectives and evidence!"); 
       this.dispose(); 
      } 
      catch(SQLException e) 
      { 
       System.out.println(e); 
      } 
     } 
     else 
     { 
      int crime = CrimeCB.getSelectedIndex(); 
      int crimeclass; 
      if(ClassCB.isEnabled() == true) 
      { 
       crimeclass = ClassCB.getSelectedIndex(); 
      } 
      else 
      { 
       crimeclass = 0; 
      } 
      String subject = SubjectTF.getText(); 
      int month = MonthCB.getSelectedIndex()+1; 
      int day = DayCB.getSelectedIndex()+1; 
      int year = Integer.parseInt(spmodel.getValue().toString()); 
      Date date = ToSQLClass.toSqlDate(year, month, day); 
      int crimetype = TypeCB.getSelectedIndex(); 
      LocationSItem li = (LocationSItem)LocationCB.getSelectedItem(); 
      int locationid = li.id; 
      String assignedDA = AssignedDATF.getText(); 
      String description = DescriptionTA.getText(); 
      String fr = FRNameTF.getText(); 
      try{ 
      Statement addstat = Econn.createStatement();    
      addstat.executeUpdate("UPDATE casefile SET subject = '" + subject 
         + "' ,assignedDA = '" + assignedDA 
         + "' ,description = '" + description 
         + "' , fr_id = '" + fr 
         +"',date = '"+date 
         +"' ,crime = "+crime 
         +" ,crimeclass = " +crimeclass 
         +" ,crimetype = " +crimetype 
         +" ,lid = " +locationid 
         + " WHERE cf_id = " + this.cfid);   
      } 
      catch(SQLException e) { System.out.println(e); } 
     } 
    }                       
    private void getSelectedDistrict() 
    { 
     try 
     { 
      LocationModel.removeAllElements(); 
      DistrictItem di = (DistrictItem)DistrictCB.getSelectedItem(); 
      Statement userStat = Econn.createStatement(); 
      String userquery = "SELECT l_id, name FROM location WHERE distr_ic = "+di.id; 
      ResultSet userResult = userStat.executeQuery(userquery); 
      while(userResult.next()) 
      { 
       int ids = userResult.getInt("l_id"); 
       String name = userResult.getString("name"); 
       LocationModel.addElement(new LocationSItem(ids,name)); 
      } 
      LocationCB.setModel(LocationModel); 
     } 
     catch(SQLException e) 
     { 
      System.out.println(e); 
     } 
    } 
    private void DistrictCBItemStateChanged(java.awt.event.ItemEvent evt) {            
     getSelectedDistrict(); 
    }           

    private void CrimeCBItemStateChanged(java.awt.event.ItemEvent evt) {           
     if(CrimeCB.getSelectedIndex() == 2) 
     { 
      ClassCB.setEnabled(true); 
     } 
     else 
     { 
      ClassCB.setEnabled(false); 
     } 
    }     
} 

的DistrictModel包含來自setDistricts()方法檢索到的每一個區域。

我認爲這是相當自我解釋它做什麼。事情是,它從來沒有進入if語句(即使它打印de districtmodel.getelementat中的所有項目),所以它通過for循環正確運行。

+2

那麼就意味着沒有模型的元素等於到從數據庫中檢索的名稱。請尊重Java命名約定。變量以小寫字母開頭。 – 2013-03-02 16:38:03

+0

那麼它所做的就是獲取p處的元素,並檢查它是否等於db檢索的名稱。所以它貫穿所有元素可能性的權利? – Kraishan 2013-03-02 19:40:29

+0

對。這就是我所說的。如果它貫穿所有元素,並且「Done << <''從不打印」,那就意味着模型中的所有元素都不會與名稱相同。 – 2013-03-02 21:29:01

回答

0

好的。現在我們可以看到代碼,答案很簡單。該區模型包含的DistrictItem實例:

DistrictModel.addElement(new DistrictItem(name,ids)); 

你這個模型中的每個元素比較的String。所以顯然,比較將始終是錯誤的,因爲一個DistrictItem不能等於一個String。只有一個字符串可以等於一個字符串。

因此,假設小區名稱是唯一的(但你應該使用ID而不是名稱),你應該通過元素迭代,並發現其具有相同的名稱檢索到的一個一個:

int i = DistrictModel.getSize(); 
for (int p = 0; p < i; p++) { 
    DistrictItem currentDistrictItem = (DistrictItem) DistrictModel.getElementAt(p); 
    if (currentDistrictItem.getName().equals(name)) { 
     DistrictCB.setSelectedIndex(p); 
     break; 
    } 
} 

此外,你應該尊重的Java命名約定:變量以lwoercase信:districtModel代替DistrictModel(和同所有其它變量)