我试图找到一个解决方案,直接为我的可编辑单元格保存数据,而无需按Enter键进行保存。大多数解决方案都是在没有双击的情况下开始编辑(我实现了这一点),我没有看到没有输入键保存数据的解决方案。 所以基于我的知识和研究,我没有找到任何可以帮助我的东西,这是我要求的可能以及如何做到这一点?
编辑:
@FXML
private TableView tableVeiwResults;
@FXML
private TableColumn colAnalysis;
@FXML
private TableColumn colResult;
private AnalysisPacientDetailsJpaController apdjpa = new
AnalysisPacientDetailsJpaController(emf);
colAnalysis.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<AnalysisPacientDetails, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<AnalysisPacientDetails, String> param) {
return new SimpleStringProperty((param.getValue()).getIdAnalysis().getAnalysisName());
}
});
colResult.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<AnalysisPacientDetails, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<AnalysisPacientDetails, String> param) {
return new SimpleStringProperty((param.getValue()).getAnalysisPacientDetailsResults());
}
});
colResult.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AnalysisPacientDetails, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<AnalysisPacientDetails, String> event) {
try {
String newValue = event.getNewValue();
if (newValue == null || newValue.trim().isEmpty()) {
return;
}
AnalysisPacientDetails apdRes = (AnalysisPacientDetails) tableVeiwResults.getSelectionModel().getSelectedItem();
apdRes.setAnalysisPacientDetailsRezultat(event.getNewValue());
apdjpa.edit(apdRes);
tableVeiwResults.refresh();
TablePosition pos = tableVeiwResults.getFocusModel().getFocusedCell();
if (pos.getRow() < tableVeiwResults.getItems().size() - 1) {
tableVeiwResults.getSelectionModel().clearAndSelect(pos.getRow() + 1, pos.getTableColumn());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
答案 0 :(得分:2)
如果我理解你的问题,你想让数据提交到单元格而不按Enter(即用户点击标签)。
我们创建了自己的自定义表格单元来处理这个问题。这是一个例子。
import javafx.beans.value.ChangeListener;
import javafx.event.Event;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
public class EditableStringTableCell<T, E> extends TableCell<T, String> {
protected TextField textField;
protected ChangeListener<? super Boolean> changeListener = (obs,ov, nv) -> {
if (!nv) {
commitEdit(textField.getText());
}
};
public EditableStringTableCell () {
}
@Override
public void startEdit() {
if(editableProperty().get()){
if (!isEmpty()) {
super.startEdit();
createTextField();
setText(null);
setGraphic(textField);
textField.requestFocus();
}
}
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText((String) getItem());
setGraphic(null);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
textField.selectAll();
}
setText(null);
setGraphic(textField);
} else {
setText(getString());
setGraphic(null);
}
}
}
protected void createTextField() {
textField = new TextField(getString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
textField.focusedProperty().addListener(changeListener);
textField.setOnAction(evt -> commitEdit(textField.getText()));
textField.setOnKeyPressed((ke) -> {
if (ke.getCode().equals(KeyCode.ESCAPE)) {
textField.focusedProperty().removeListener(changeListener);
cancelEdit();
}
if (ke.getCode().equals(KeyCode.TAB)) {
commitEdit(textField.getText());
}
});
}
protected String getString() {
return getItem() == null ? "" : getItem().toString();
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void commitEdit(String item) {
textField.focusedProperty().removeListener(changeListener);
if (isEditing()) {
super.commitEdit(item);
} else {
final TableView table = getTableView();
if (table != null) {
TablePosition position = new TablePosition(getTableView(),
getTableRow().getIndex(), getTableColumn());
CellEditEvent editEvent = new CellEditEvent(table, position,
TableColumn.editCommitEvent(), item);
Event.fireEvent(getTableColumn(), editEvent);
}
updateItem(item, false);
if (table != null) {
table.edit(-1, null);
}
}
}
}