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();
}
}
}
您使用的是什麼JRE/JDK版本?還有一個類似的bug在最新版本(8u101或8u111 IIRC) – Itai
中得到修復非常感謝。 (java -version – gbear
版本是8u-60。很快會在測試中升級,非常感謝。@sillyfly – gbear