1

I tried to find a solution to save data for my editable cell direct without pressing enter to save it. Most solutions were for starting editing without double click(I implemented that) and I saw no solution for saving data without enter key. So based on my knowledge and research I didn't find anything to help me, is this what I'm asking possible and how to do it?

Edited:

@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();
            }
  }
});
SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • Not without a serious amount of complex hacking: see https://stackoverflow.com/questions/24694616/how-to-enable-commit-on-focuslost-for-tableview-treetableview – James_D Apr 12 '18 at 11:04
  • Hi @James_D, thanks for solution you sent me. I checked it but i'm having difficulties to implement it. I edited my post and added my code. Can you have a look and to help me with it – Bardh Krasniqi Apr 12 '18 at 12:24

1 Answers1

3

If I understand your question you want to have the data commit to the cell without hitting enter (i.e. user hits tab).

We created our own custom table cells to handle this. Here is an example.

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);
            }

        }
    }

}
purring pigeon
  • 4,141
  • 5
  • 35
  • 68
  • Yeah that is what i'm asking for – Bardh Krasniqi Apr 12 '18 at 13:33
  • Will you accept this as the correct answer then? That way others can find it. – purring pigeon Apr 12 '18 at 14:08
  • Hey @purring pigeon i'm trying to add a new function in your code. When Key.ENTER is pressed i want to save value on selected cell and to go to next row to start editing cell. I tried to do this way but i failed only its highlighting cell and when i click on cell(new one) it's automaticly saving value of last cell that was commited. Code i added is inside of textField.setOnKeypress(...); – Bardh Krasniqi Apr 17 '18 at 12:42
  • Code: `if (ke.getCode().equals((KeyCode.ENTER))) { commitEdit(textField.getText()); TablePosition position = new TablePosition(getTableView(), getTableRow().getIndex(), getTableColumn()); if (position.getRow() < getTableView().getItems().size() - 1) { getTableView().getSelectionModel().clearAndSelect(position.getRow() + 1, position.getTableColumn()); startEdit(); } }` – Bardh Krasniqi Apr 17 '18 at 12:42
  • That is a whole different issue - might be best to create a new question for that, as the answer there lies in the table, rather than the cell. – purring pigeon Apr 17 '18 at 14:59