Issue:
When you select a table row, and then further click inside the selected row but in a different column (so you're selecting a different cell, basically click back and forth between the two columns in the test case but within the selected row), the SelectedItem gets set to null.
Code to reproduce:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class TableSelectionBug extends Application {
@Override
public void start(final Stage stage) throws Exception {
TableView<OneRow> table = new TableView<OneRow>();
table.getSelectionModel().setCellSelectionEnabled(false);
table.setEditable(false);
table.getSelectionModel().selectedItemProperty().addListener((prop, ov, nv)-> System.out.println("Selected item is: " + nv));
final ObservableList<OneRow> tableData = FXCollections.synchronizedObservableList(FXCollections.observableArrayList());
for (int i = 0; i < 30; i++) {
tableData.add(new OneRow("Name #" + i, "Surname #" + i));
}
TableColumn<OneRow, String> col1 = new TableColumn<OneRow, String>("First Name");
col1.setCellValueFactory(new PropertyValueFactory<OneRow, String>("firstName"));
TableColumn<OneRow, String> col2 = new TableColumn<OneRow, String>("Last Name");
col2.setCellValueFactory(new PropertyValueFactory<OneRow, String>("lastName"));
table.setItems(tableData);
table.getColumns().add(col1);
table.getColumns().add(col2);
Scene mainScene = new Scene(table, 800, 600);
stage.setScene(mainScene);
stage.setTitle("Select a row in the table, then click around on different columns within the row.");
stage.show();
stage.setOnCloseRequest(e -> {
Platform.exit();
System.exit(0);
});
}
public static void main(final String[] args) {
launch();
}
public class OneRow {
public OneRow(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
private StringProperty _firstNameProperty;
private StringProperty _lastNameProperty;
public StringProperty firstNameProperty() {
if (_firstNameProperty == null) {
_firstNameProperty = new SimpleStringProperty();
}
return _firstNameProperty;
}
public void setFirstName(String firstName) {
firstNameProperty().set(firstName);
}
public String getFirstName() {
return firstNameProperty().get();
}
public StringProperty lastNameProperty() {
if (_lastNameProperty == null) {
_lastNameProperty = new SimpleStringProperty();
}
return _lastNameProperty;
}
public void setLastName(String lastName) {
lastNameProperty().set(lastName);
}
public String getLastName() {
return lastNameProperty().get();
}
}
}
When you select a table row, and then further click inside the selected row but in a different column (so you're selecting a different cell, basically click back and forth between the two columns in the test case but within the selected row), the SelectedItem gets set to null.
Code to reproduce:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class TableSelectionBug extends Application {
@Override
public void start(final Stage stage) throws Exception {
TableView<OneRow> table = new TableView<OneRow>();
table.getSelectionModel().setCellSelectionEnabled(false);
table.setEditable(false);
table.getSelectionModel().selectedItemProperty().addListener((prop, ov, nv)-> System.out.println("Selected item is: " + nv));
final ObservableList<OneRow> tableData = FXCollections.synchronizedObservableList(FXCollections.observableArrayList());
for (int i = 0; i < 30; i++) {
tableData.add(new OneRow("Name #" + i, "Surname #" + i));
}
TableColumn<OneRow, String> col1 = new TableColumn<OneRow, String>("First Name");
col1.setCellValueFactory(new PropertyValueFactory<OneRow, String>("firstName"));
TableColumn<OneRow, String> col2 = new TableColumn<OneRow, String>("Last Name");
col2.setCellValueFactory(new PropertyValueFactory<OneRow, String>("lastName"));
table.setItems(tableData);
table.getColumns().add(col1);
table.getColumns().add(col2);
Scene mainScene = new Scene(table, 800, 600);
stage.setScene(mainScene);
stage.setTitle("Select a row in the table, then click around on different columns within the row.");
stage.show();
stage.setOnCloseRequest(e -> {
Platform.exit();
System.exit(0);
});
}
public static void main(final String[] args) {
launch();
}
public class OneRow {
public OneRow(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
private StringProperty _firstNameProperty;
private StringProperty _lastNameProperty;
public StringProperty firstNameProperty() {
if (_firstNameProperty == null) {
_firstNameProperty = new SimpleStringProperty();
}
return _firstNameProperty;
}
public void setFirstName(String firstName) {
firstNameProperty().set(firstName);
}
public String getFirstName() {
return firstNameProperty().get();
}
public StringProperty lastNameProperty() {
if (_lastNameProperty == null) {
_lastNameProperty = new SimpleStringProperty();
}
return _lastNameProperty;
}
public void setLastName(String lastName) {
lastNameProperty().set(lastName);
}
public String getLastName() {
return lastNameProperty().get();
}
}
}
- duplicates
-
JDK-8095446 [TableView] IOOBE when using bindings API with SelectionModel selectedItems list
- Resolved
- relates to
-
JDK-8094653 [TableView] selectedItemProperty is fired when we select different cells in the same row, with the event's new value being null
- Closed