1
votes

using the latest java-fx version I created a ListView with SelectionMode.MULTIPLE, inserted some data and attached a ChangeListener as follows:

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

No when I select some items, every selection change is notified. When I then deselect single items with ctrl-mouse clicks, no selection change events are fired, only when the last formerly selected item is deselected, I receive the event. What am I doing wrong?

best regards Hans

1
No time to check right now, but I believe this is a bug that is fixed in 1.8.0u60 - James_D
The answer of NwDx is perfect. Up to now I missed the existence of ListChangeListeners and wondered about the naming of selectedItemProperty. It is quite error-prone since many code examples with changelisteners on selectedItemProperties are around and they seem to work at first sight. - Hans Huckebein

1 Answers

2
votes

Problem

How should a selectedItemProperty reflect a multi row selection? It can't, because it was intended to use with SelectionMode.SINGLE.

Solution

You can use a ListChangeListener on your TableView or ListView instead. It's the recommended way in multiple selection models. With this you be able to know which row(-s) where deleted, updated, removed, etc. to the selection.

I've made an example based on the Table View Tutorial from Oracle. Don't be scared, it's a bit huge, but this lies in nature of Lists and Tables. I've marked the interesting sections with a comment.

Example

Here is a 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", "[email protected]"),
          new Person("Isabella", "Johnson", "[email protected]"),
          new Person("Ethan", "Williams", "[email protected]"),
          new Person("Emma", "Jones", "[email protected]"),
          new Person("Michael", "Brown", "[email protected]")
      );

  @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() + '}';
    }
  }
}

Example output to console

######- Items -########
Added: Person{first name=Michael, last name=Brown, [email protected]}
--------------------------
Removed: Person{first name=Isabella, last name=Johnson, [email protected]}
--------------------------
Removed: Person{first name=Ethan, last name=Williams, [email protected]}
--------------------------
Removed: Person{first name=Emma, last name=Jones, [email protected]}
--------------------------
Removed: Person{first name=Jacob, last name=Smith, [email protected]}
--------------------------
#######- Indicies -#######
Added: 4
--------------------------
Removed: 1
--------------------------
Removed: 2
--------------------------
Removed: 3
--------------------------
Removed: 0
--------------------------