ตัวจัดการเหตุการณ์เมื่อแถวถูกลบออกจากมุมมองตาราง

ฉันจำเป็นต้องรู้วิธีจับเหตุการณ์ที่เกิดขึ้นเมื่อแถวถูกลบออกจาก TableView และดัชนีของแถว ในขณะนี้เมื่อแถวถูกลบออกจากมุมมองตาราง จะมีการเรียกเมธอด TableView.getSelectionModel().clearSelection() แต่ฉันต้องการคือเลือกดัชนีสุดท้ายที่มีอยู่ในมุมมองตาราง

Tableview.getSelectionModel().clearAndSelect() ไม่ใช่ตัวเลือก เนื่องจากบางครั้งแถวจะถูกลบโดยอัตโนมัติ

ความนับถือ


person Pavel Delgado    schedule 19.04.2016    source แหล่งที่มา


คำตอบ (1)


สำหรับตารางที่มีประเภท เช่น Person:

import javafx.collections.ListChangeListener.Change ;

// ....

TableView<Person> table = ... ;

table.getItems().addListener((Change<? extends Person> c) -> {
    while(c.next()) {
        if (c.wasRemoved()) {
            int numRemoved = c.getRemoved().size();
            int index = c.getFrom();
            System.out.println(numRemoved + " items removed from table at index "+index);
        }
    }
});

ListChangeListener.Change เอกสาร อธิบายค่าที่ส่งคืน โดย c.getFrom(), c.getTo(), c.wasRemoved(), c.getAdded() ฯลฯ ภายใต้สถานการณ์ต่างๆ

person James_D    schedule 19.04.2016
comment
ใช้งานได้ แต่นิพจน์ที่ถูกต้องคือ table.getItems().addListener((ListChangeListener.Change‹? ขยาย Person› c) -› {}); กรุณาแก้ไข. ความนับถือ. - person Pavel Delgado; 20.04.2016
comment
โอเค เข้าใจแล้ว ขอบคุณ !! - person Pavel Delgado; 20.04.2016