使用 Ctrl+ 鼠标左键(取消)选择多个项目时,未从 ListView 获取所有选择更改事件

not getting all selection change events from ListView while (de-)selecting multiple items with Ctrl+ left mouse key

使用最新的 java-fx 版本,我使用 SelectionMode.MULTIPLE 创建了一个 ListView,插入了一些数据并附加了一个 ChangeListener,如下所示:

myTableView.getSelectionModel().selectedItemProperty().addListener(this);

不,当我 select 某些项目时,每次 selection 更改都会收到通知。然后当我用 ctrl-mouse clicks deselect 单个项目时,没有 selection change 事件被触发,只有当最后一个以前 selected 项目被 deselected 时,我收到了这个事件。我做错了什么?

此致 汉斯

问题

selectedItemProperty 应如何反映多行选择?它不能,因为它打算与 SelectionMode.SINGLE.

一起使用

解决方案

您可以在 TableView 或 ListView 上使用 ListChangeListener。这是多选模型中的推荐方式。有了这个,您就可以知道在选择中删除、更新、删除等的行(-s)。

我已经根据来自 Oracle 的 Table View Tutorial 做了一个例子。不要害怕,它有点大,但这是列表和表格的本质。我已经用评论标记了有趣的部分。

例子

这是一个Minimal, Complete, and Verifiable example

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewDemo extends Application {

  private final TableView<Person> table = new TableView<>();
  private final ObservableList<Person> data
      = FXCollections.<Person>observableArrayList(
          new Person("Jacob", "Smith", "jacob.smith@example.com"),
          new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
          new Person("Ethan", "Williams", "ethan.williams@example.com"),
          new Person("Emma", "Jones", "emma.jones@example.com"),
          new Person("Michael", "Brown", "michael.brown@example.com")
      );

  @Override
  public void start(Stage primaryStage) {
    Scene scene = new Scene(new Group());
    primaryStage.setTitle("Table View Sample");
    primaryStage.setWidth(450);
    primaryStage.setHeight(500);

    final Label label = new Label("Address Book");
    label.setFont(new Font("Arial", 20));

    table.setEditable(true);
    table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

    // begin of interesting code
    table.getSelectionModel().getSelectedIndices().addListener(new ListChangeListener<Integer>() {

      @Override
      public void onChanged(ListChangeListener.Change<? extends Integer> c) {
        while (c.next()) {
          System.out.println("#######- Indicies -#######");
          if (c.wasAdded()) {
            c.getAddedSubList().stream().forEach((item) -> {
              System.out.println("Added: " + item);
              System.out.println("--------------------------");
            });
          }
          if (c.wasRemoved()) {
            c.getRemoved().stream().forEach((item) -> {
              System.out.println("Removed: " + item);
              System.out.println("--------------------------");
            });
          }
          //c.wasPermutated();
          //c.wasReplaced();
        }
      }
    });

    table.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<Person>() {

      @Override
      public void onChanged(ListChangeListener.Change<? extends Person> c) {
        while (c.next()) {
          System.out.println("######- Items -########");
          if (c.wasAdded()) {
            c.getAddedSubList().stream().forEach((item) -> {
              System.out.println("Added: " + item);
              System.out.println("--------------------------");
            });
          }
          if (c.wasRemoved()) {
            c.getRemoved().stream().forEach((item) -> {
              System.out.println("Removed: " + item);
              System.out.println("--------------------------");
            });
          }
          //c.wasPermutated();
          //c.wasReplaced();
        }
      }
    });

    // end of interesting code
    TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
    firstNameCol.setMinWidth(100);
    firstNameCol.setCellValueFactory(
        new PropertyValueFactory<>("firstName"));

    TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
    lastNameCol.setMinWidth(100);
    lastNameCol.setCellValueFactory(
        new PropertyValueFactory<>("lastName"));

    TableColumn<Person, String> emailCol = new TableColumn<>("Email");
    emailCol.setMinWidth(200);
    emailCol.setCellValueFactory(
        new PropertyValueFactory<>("email"));

    table.setItems(data);
    // I know this could be done by invoking addAll, but Xlint will give warning
    table.getColumns().add(firstNameCol);
    table.getColumns().add(lastNameCol);
    table.getColumns().add(emailCol);

    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10, 0, 0, 10));
    vbox.getChildren().addAll(label, table);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }

  public static class Person {

    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty email;

    private Person(String fName, String lName, String email) {
      this.firstName = new SimpleStringProperty(fName);
      this.lastName = new SimpleStringProperty(lName);
      this.email = new SimpleStringProperty(email);
    }

    public String getFirstName() {
      return firstName.get();
    }

    public void setFirstName(String fName) {
      firstName.set(fName);
    }

    public String getLastName() {
      return lastName.get();
    }

    public void setLastName(String fName) {
      lastName.set(fName);
    }

    public String getEmail() {
      return email.get();
    }

    public void setEmail(String fName) {
      email.set(fName);
    }

    @Override
    public String toString() {
      return "Person{" + "first name=" + firstName.get()
             + ", last name=" + lastName.get()
             + ", email=" + email.get() + '}';
    }
  }
}

控制台输出示例

######- Items -########
Added: Person{first name=Michael, last name=Brown, email=michael.brown@example.com}
--------------------------
Removed: Person{first name=Isabella, last name=Johnson, email=isabella.johnson@example.com}
--------------------------
Removed: Person{first name=Ethan, last name=Williams, email=ethan.williams@example.com}
--------------------------
Removed: Person{first name=Emma, last name=Jones, email=emma.jones@example.com}
--------------------------
Removed: Person{first name=Jacob, last name=Smith, email=jacob.smith@example.com}
--------------------------
#######- Indicies -#######
Added: 4
--------------------------
Removed: 1
--------------------------
Removed: 2
--------------------------
Removed: 3
--------------------------
Removed: 0
--------------------------