I created a TableView with a single <String, String> column that can be edited and I used the TextFieldTableCell.forTableColumn() as cell factory.
This cell activates edit mode by double click, F2, Space and Enter key and commits the edited value on Enter key.
The issue is that If I single click the cell and hit the Enter key to activate edit mode, the TextField is shown for a brief moment and then disappears. The looks like the KeyEvent is first handled by the Behavior showing the TextField and immediately after it's handled by the TextField itself, triggering the commit and hiding the field.
A secondary possible issue happens when hitting the Space key. The TextField is shown and its content is immediately deleted as result of selectAll, requestFocus and Space key event. The issie is that a leading white space is set as content of the TextField so, if I just hit Space on the cell and start typing "My Name", the content of the TextField will be " My Name".
Here is the code I used:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewSample extends Application {
private final ObservableList<String> data = FXCollections.observableArrayList("first", "second", "third");
private TableView<String> table = new TableView<>();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
table.setEditable(true);
TableColumn<String, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<String, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<String, String> param) {
if (param.getValue() != null) {
return new SimpleStringProperty(param.getValue());
}
return null;
}
});
firstNameCol.setEditable(true);
firstNameCol.setOnEditCommit(t -> {
t.getTableView().getItems().set(t.getTablePosition().getRow(), t.getNewValue());
});
table.getColumns().addAll(firstNameCol);
table.setItems(data);
final VBox vbox = new VBox();
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(table);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
}
This cell activates edit mode by double click, F2, Space and Enter key and commits the edited value on Enter key.
The issue is that If I single click the cell and hit the Enter key to activate edit mode, the TextField is shown for a brief moment and then disappears. The looks like the KeyEvent is first handled by the Behavior showing the TextField and immediately after it's handled by the TextField itself, triggering the commit and hiding the field.
A secondary possible issue happens when hitting the Space key. The TextField is shown and its content is immediately deleted as result of selectAll, requestFocus and Space key event. The issie is that a leading white space is set as content of the TextField so, if I just hit Space on the cell and start typing "My Name", the content of the TextField will be " My Name".
Here is the code I used:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewSample extends Application {
private final ObservableList<String> data = FXCollections.observableArrayList("first", "second", "third");
private TableView<String> table = new TableView<>();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
table.setEditable(true);
TableColumn<String, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<String, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<String, String> param) {
if (param.getValue() != null) {
return new SimpleStringProperty(param.getValue());
}
return null;
}
});
firstNameCol.setEditable(true);
firstNameCol.setOnEditCommit(t -> {
t.getTableView().getItems().set(t.getTablePosition().getRow(), t.getNewValue());
});
table.getColumns().addAll(firstNameCol);
table.setItems(data);
final VBox vbox = new VBox();
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(table);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
}
- relates to
-
JDK-8273633 ListView et al: start edit with SPACE must not delete editor content
-
- Open
-