使用最新的java-fx版本我使用SelectionMode.MULTIPLE创建了一个ListView,插入了一些数据并附加了一个ChangeListener,如下所示:
myTableView.getSelectionModel().selectedItemProperty().addListener(this);
否,当我选择一些项目时,会通知每个选择更改。然后,当我通过ctrl-mouse单击取消选择单个项目时,不会触发选择更改事件,仅当取消选择上一个以前选择的项目时,才会收到该事件。我做错了什么?
最好的问候 汉斯答案 0 :(得分:2)
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
--------------------------