2016-11-09 46 views
0

我有一個使用getSelectedIndices()的問題。偶爾,並非總是如我在數據庫代碼中的調試會話中發現的那樣,它會返回一個包含索引-1的數組。java TableView getSelectedIndices()包含索引-1

爲什麼會這樣,我該怎麼做才能防止這種情況?我想用這種風格的邏輯是各種各樣的地方。

我有一個LiveMovieViewView SelectionMode.MULTIPLE。 它有一個文本菜單,其包括(「設置品種」,「設置性別」和「組具有小牛」)

設置這些屬性中的一個的邏輯是相同的: 用戶將(1),選擇相關的行(使用或+鼠標點擊),則(2)選擇相關的菜單項

然後這將調用setLivestockAttribute()如下:

private void setLivestockAttribute(String attribute) 
{ 
    String value = ""; 

    // 1. Get a List of the Indices of Rows selected. 
    ObservableList<Integer> selectedIndices = null; 
    selectedIndices = FXCollections.observableList(tblView_Livestock.getSelectionModel().getSelectedIndices());   

    // If ONE or MORE rows have been selected. 
    if (selectedIndices.size() > 0) 
    { 
     // 2. Get the VALUE of the relevant ATTRIBUTE. 
     switch (attribute) 
     { 
      case LM_Constant.BREED: 
       value = LM_Utility.getChoice_Breed(); 
       break; 
      case LM_Constant.SEX: 
       value = LM_Utility.getChoice_Sex(); 
       break; 
      case LM_Constant.HAS_CALF: 
       value = LM_Utility.getChoice_HasCalf(); 
       break;      
     } 

     // If there is a VALUE. 
     if (value.length() > 0) 
     { 
      ObservableList<LivestockModel> dataList = tblView_Livestock.getItems(); 

      // 3. Update each Livestock record. 
      DataStore.getInstance().updateLivestock(dataList, selectedIndices, attribute, value); 

      // 4. Refresh the TableView to show changes. 
      setLivestockData();  
     } 
    } 
} 
+0

您使用的是什麼JRE/JDK版本?還有一個類似的bug在最新版本(8u101或8u111 IIRC) – Itai

+0

中得到修復非常感謝。 (java -version – gbear

+0

版本是8u-60。很快會在測試中升級,非常感謝。@sillyfly – gbear

回答

0

我已決定:

  • 更新JDK(如由@sillyfly建議的)
  • 調整代碼以避免錯誤(見下文)
  • 訂閱甲骨文接收未來升級的通知和錯誤修正

代碼段保證數據庫只有當索引i> = 0時纔會更新。

public boolean updateLivestock(ObservableList<LivestockModel> livestockData, ObservableList<Integer> selectedIndices, String attribute, String value) 
{ 
    boolean proceed = true; 
    boolean updated = false; 
    int rowCount = 0; 
    int counter = 0; 
    String sql = ""; 
    LivestockModel lm; 

    switch (attribute) 
    { 
     case LM_Constant.BREED: 
      sql = "UPDATE livestock SET (breed, last_updated) = (?, DEFAULT) WHERE rfid = ? AND begin_event = ?"; 
      break; 
     case LM_Constant.SEX: 
      sql = "UPDATE livestock SET (sex, last_updated) = (?, DEFAULT) WHERE rfid = ? AND begin_event = ?"; 
      break; 
     case LM_Constant.HAS_CALF: 
      sql = "UPDATE livestock SET (has_calf, last_updated) = (?, DEFAULT) WHERE rfid = ? AND begin_event = ?"; 
      break;     
    } 

    try (PreparedStatement preparedUpdate = connection.prepareStatement(sql)) 
    {    
     for (Integer i : selectedIndices) 
     { 
      if (proceed != true) 
       break; 

      if (i >= 0) 
      { 
       lm = livestockData.get(i); 

       preparedUpdate.setString(1, value); 
       preparedUpdate.setString(2, lm.getRFID()); 
       preparedUpdate.setInt(3, lm.getBeginEvent()); 

       rowCount = preparedUpdate.executeUpdate(); 
       if (rowCount == 1) 
        counter++; 
      } 
      else 
      { 
       counter++; 
       LM_Utility.showError_Dialog("Update Livestock", "Index Error", "index = " + i.toString()); 
      } 
     } 

     if (counter == selectedIndices.size()) 
     { 
      connection.commit();    
      updated = true; 
     } 

    } 
    catch (SQLException sqle) 
    { 
     LM_Utility.showSQL_Exception("updateLivestock()", sqle); 
     proceed = false; 
    }   

    return updated; 
}