-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
8
-
Windows 8 64bit
After sorting (or adding new rows to) the TableView, and trying to delete a row using the following code, another row gets deleted instead!
table.setOnKeyReleased(event -> {
if (event.getCode().equals(KeyCode.DELETE)) {
persons.remove(table.getSelectionModel().getSelectedIndex());
}
});
However, the TextFieldTableCell works well in editing, i.e. when editing the value of the cell, the correct item in the underlying ObservableList gets updated with the new value.
Here is the SSCCE:
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* Author: Anas H. Sulaiman (anas@ahs.pw)
*/
public class CheckBoxTableCellBug extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Sami", "Haddad", false));
persons.add(new Person("Ahmed", "Hasan", true));
persons.add(new Person("Rami", "Kassar", true));
persons.add(new Person("Nehad", "Hamad", false));
persons.add(new Person("Jamal", "Raei", true));
persons.add(new Person("Ameer", "Raji", true));
persons.add(new Person("Tahseen", "Muhsen", true));
SortedList<Person> sortedList = new SortedList<>(persons);
TableView<Person> table = new TableView<>(sortedList);
sortedList.comparatorProperty().bind(table.comparatorProperty());
table.setEditable(true);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
TableColumn<Person, String> colFirstName = new TableColumn<>("First Name");
colFirstName.setCellValueFactory(p -> p.getValue().firstName);
colFirstName.setCellFactory(TextFieldTableCell.forTableColumn());
colFirstName.setOnEditCommit(event -> event.getTableView().getItems().get(event.getTablePosition().getRow()).firstName.set(event.getNewValue()));
TableColumn<Person, String> colLastName = new TableColumn<>("Last Name");
colLastName.setCellValueFactory(p -> p.getValue().lastName);
colLastName.setCellFactory(TextFieldTableCell.forTableColumn());
colLastName.setOnEditCommit(event -> event.getTableView().getItems().get(event.getTablePosition().getRow()).lastName.set(event.getNewValue()));
TableColumn<Person, Boolean> colInvited = new TableColumn<>("Invited");
colInvited.setCellValueFactory(p -> p.getValue().invited);
colInvited.setCellFactory(CheckBoxTableCell.forTableColumn(colInvited));
table.getColumns().addAll(colFirstName, colLastName, colInvited);
table.setOnKeyReleased(event -> {
if (event.getCode().equals(KeyCode.DELETE)) {
persons.remove(table.getSelectionModel().getSelectedIndex());
}
});
stage.setScene(new Scene(new StackPane(table)));
stage.setTitle("CheckBoxTableCell Bug");
stage.show();
}
class Person {
public StringProperty firstName;
public StringProperty lastName;
public BooleanProperty invited;
public Person() {
this.firstName = new SimpleStringProperty("");
this.lastName = new SimpleStringProperty("");
this.invited = new SimpleBooleanProperty(false);
}
public Person(String fname, String lname, boolean invited) {
this();
this.firstName.set(fname);
this.lastName.set(lname);
this.invited.set(invited);
}
}
}
To reproduce the bug:
1. Click once on the "First Name" column header.
2. Select the second row.
3. Delete the row by pressing (and releasing) the DELETE key.
4. Notice that the first row gets deleted instead.
table.setOnKeyReleased(event -> {
if (event.getCode().equals(KeyCode.DELETE)) {
persons.remove(table.getSelectionModel().getSelectedIndex());
}
});
However, the TextFieldTableCell works well in editing, i.e. when editing the value of the cell, the correct item in the underlying ObservableList gets updated with the new value.
Here is the SSCCE:
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* Author: Anas H. Sulaiman (anas@ahs.pw)
*/
public class CheckBoxTableCellBug extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Sami", "Haddad", false));
persons.add(new Person("Ahmed", "Hasan", true));
persons.add(new Person("Rami", "Kassar", true));
persons.add(new Person("Nehad", "Hamad", false));
persons.add(new Person("Jamal", "Raei", true));
persons.add(new Person("Ameer", "Raji", true));
persons.add(new Person("Tahseen", "Muhsen", true));
SortedList<Person> sortedList = new SortedList<>(persons);
TableView<Person> table = new TableView<>(sortedList);
sortedList.comparatorProperty().bind(table.comparatorProperty());
table.setEditable(true);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
TableColumn<Person, String> colFirstName = new TableColumn<>("First Name");
colFirstName.setCellValueFactory(p -> p.getValue().firstName);
colFirstName.setCellFactory(TextFieldTableCell.forTableColumn());
colFirstName.setOnEditCommit(event -> event.getTableView().getItems().get(event.getTablePosition().getRow()).firstName.set(event.getNewValue()));
TableColumn<Person, String> colLastName = new TableColumn<>("Last Name");
colLastName.setCellValueFactory(p -> p.getValue().lastName);
colLastName.setCellFactory(TextFieldTableCell.forTableColumn());
colLastName.setOnEditCommit(event -> event.getTableView().getItems().get(event.getTablePosition().getRow()).lastName.set(event.getNewValue()));
TableColumn<Person, Boolean> colInvited = new TableColumn<>("Invited");
colInvited.setCellValueFactory(p -> p.getValue().invited);
colInvited.setCellFactory(CheckBoxTableCell.forTableColumn(colInvited));
table.getColumns().addAll(colFirstName, colLastName, colInvited);
table.setOnKeyReleased(event -> {
if (event.getCode().equals(KeyCode.DELETE)) {
persons.remove(table.getSelectionModel().getSelectedIndex());
}
});
stage.setScene(new Scene(new StackPane(table)));
stage.setTitle("CheckBoxTableCell Bug");
stage.show();
}
class Person {
public StringProperty firstName;
public StringProperty lastName;
public BooleanProperty invited;
public Person() {
this.firstName = new SimpleStringProperty("");
this.lastName = new SimpleStringProperty("");
this.invited = new SimpleBooleanProperty(false);
}
public Person(String fname, String lname, boolean invited) {
this();
this.firstName.set(fname);
this.lastName.set(lname);
this.invited.set(invited);
}
}
}
To reproduce the bug:
1. Click once on the "First Name" column header.
2. Select the second row.
3. Delete the row by pressing (and releasing) the DELETE key.
4. Notice that the first row gets deleted instead.