2016-09-27 17 views
6

Ben iki ListView s var, allStudentsList içinde zaten öğe bulundu, currentStudentList hiçbiri vardır. Kullanıcı allStudentList'da bir öğeyi seçtiğinde amacım, bu öğe için currentStudentList'a taşınmasıdır. Bunu, allStudentList seçim modeline bir dinleyici yerleştirerek yaptım.Değişim dinleyicisi ile iki ListViews arasında öğe nasıl taşınır? JavaFX

Bir IndexOutOfBoundsException alıyorum ve bunun neden olduğundan emin değilim. Testten, bu problemin bu yöntemin son 4 satırına ayrıldığı görülüyor, ancak neden emin değilim. Bir Platform.runLater(...) bloğuna öğe listeleri değiştirmek kod parçalarını sarar hızlı bir düzeltme olarak

allStudentsList.getSelectionModel().selectedItemProperty() 
     .addListener((observableValue, oldValue, newValue) -> { 
      if (allStudentsList.getSelectionModel().getSelectedItem() != null) { 

       ArrayList<String> tempCurrent = new ArrayList<>(); 
       for (String s : currentStudentList.getItems()) { 
        tempCurrent.add(s); 
       } 

       ArrayList<String> tempAll = new ArrayList<>(); 
       for (String s : allStudentsList.getItems()) { 
        tempAll.add(s); 
       } 

       tempAll.remove(newValue); 
       tempCurrent.add(newValue); 

       // clears current studentlist and adds the new list 
       if (currentStudentList.getItems().size() != 0) { 
        currentStudentList.getItems().clear(); 
       } 
       currentStudentList.getItems().addAll(tempCurrent); 

       // clears the allStudentList and adds the new list 
       if (allStudentsList.getItems().size() != 0) { 
        allStudentsList.getItems().clear(); 
       } 
       allStudentsList.getItems().addAll(tempAll); 
      } 
     }); 
+0

İstisna hangi satır atılıyor? –

+0

allStudents.getItems(). Clear(); – Philayyy

+0

allStudentsList.getItems() addAll (tempAll); – Philayyy

cevap

3

:

Platform.runLater(() -> { 
    // clears current studentlist and adds the new list 
    if (currentStudentList.getItems().size() != 0) 
     currentStudentList.getItems().clear(); 

    currentStudentList.getItems().addAll(tempCurrent); 
}); 

Platform.runLater(() -> { 
    // clears the allStudentList and adds the new list 
    if (allStudentsList.getItems().size() != 0) 
     allStudentsList.getItems().clear(); 

    allStudentsList.getItems().addAll(tempAll); 
}); 
sorun bir seçme değişikliği işlenirken Seçimi değiştiremezsiniz olmasıdır

. Tüm öğeleri allStudentsList.getItems().clear(); ile kaldırdığınızda, seçim değişecektir (seçilen dizin -1 olacaktır), belirtilen koşul karşılanacaktır. Bu, Platform.runLater(...) bloğunun kullanımının, modifikasyonu "erteleme" ile önleneceği şeydir.

Ama bütün işleyicisi

allStudentsList.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> { 
    if (newValue != null) { 

     Platform.runLater(() -> { 
      allStudentsList.getSelectionModel().select(-1); 
      currentStudentList.getItems().add(newValue); 
      allStudentsList.getItems().remove(newValue); 
     }); 
    } 
}); 

O -1 seçilen dizini ayarlar ile alınıp satılabileceği: hiçbir şey mevcut olanı çıkarılırken farklı bir öğeye değişen önlemek için ListView seçilir (bu örtük yapıldı Listenizdeki listeyi temizleyerek, o anda seçili elemanı s "seçilen liste" ye ekler, sonra mevcut öğeyi "tüm liste listesinden" kaldırır. Tüm bu eylemler belirtilen Platform.runLater(...) bloğuna sarılmıştır.

+1

Mükemmel, bir tedavi! Açıklama için alkış – Philayyy

İlgili konular