我试图提交一个TableCell
,但调用了cancelEdit()
。这只有在我通过调用tableView.edit()
使cell
进入其编辑状态时才会发生。尝试提交TableCell时调用cancelEdit()
我调用由editCommit()
:
private void handleKeyPressed(KeyEvent e) {
if (e.getCode() == KeyCode.ENTER) {
commitEdit(textField.getText());
}
}
为什么按下回车键时cancelEdit()
被称为?手动单击添加的空白行并尝试通过按Enter键提交单元格可以正常工作。
import java.util.function.Function;
import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.control.TableView;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.beans.property.SimpleStringProperty;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
root.setCenter(new TableExample());
}
public class TableExample extends VBox {
public TableExample() {
TableView<Building> tableView = new TableView<>();
tableView.setEditable(true);
//adds a new blank row to table;
Button add = new Button("add");
add.setOnAction(e -> {
Building newBuilding = new Building();
tableView.getItems().add(0, newBuilding);
tableView.edit(0, tableView.getColumns().get(0));
//I tried focusing and selecting the row
//but does not help.
tableView.getFocusModel().focus(0);
tableView.getSelectionModel().select(0);
});
getChildren().addAll(add, tableView);
Building building = new Building();
building.setName("Building 100");
Function<Building, StringProperty> property = Building::nameProperty;
TableColumn<Building, String> column = new TableColumn<>("name");
column.setEditable(true);
column.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
column.setCellFactory(p -> new EditingStringCell<>());
tableView.getColumns().add(column);
tableView.setItems(FXCollections.observableArrayList(building));
}
}
public class EditingStringCell<S, T> extends TableCell<Building, String> {
private TextField textField;
public EditingStringCell() {
setEditable(true);
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : item);
}
@Override
public void startEdit() {
System.out.println("start edit");
super.startEdit();
buildControl(getItem());
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
private void buildControl(String item) {
System.out.println("item: " + item);
textField = new TextField();
textField.addEventHandler(KeyEvent.KEY_RELEASED,
this::handleKeyPressed);
textField.setText(item == null ? "" : (item));
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(textField);
}
//whenever pressing enter on a new "blank" row
//cancelEdit() gets called. But if you try again
//it works.
private void handleKeyPressed(KeyEvent e) {
if (e.getCode() == KeyCode.ENTER) {
commitEdit(textField.getText());
}
}
@Override
public void cancelEdit() {
System.out.println("cancel edit");
super.cancelEdit();
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
@Override
public void commitEdit(String newValue) {
System.out.println("commited: " + newValue);
super.commitEdit(newValue);
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
public class Building {
private final StringProperty name = new SimpleStringProperty();
public StringProperty nameProperty() {
return name;
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
}
public static void main(String[] args) {launch(args);}
}
请问为什么你重写的CancelEdit( ) 方法?通过删除它,问题就解决了,并且更改已被提交 – JKostikiadis
@JKostikiadis如果您不按回车键单击其他单元格,则最初单击的单元格仍会显示文本字段。 CancelEdit()调用setContentDisplay(ContentDisplay.TEXT_ONLY); – jpell
所以你想检查,如果没有按下输入字段失去焦点,如果是这样,只是放弃更改..我看到让我考虑一下 – JKostikiadis