1

I want to commit changes on ListCell without enter key.

I have the following code :

objetListview1.setEditable(true);
objetListview1.setItems(FXCollections.observableArrayList(observableSet2));
objetListview1.setEditable(true);

objetListview1.setCellFactory(lv -> {
    TextFieldListCell<Typ> cell = new TextFieldListCell<Typ>();
    StringConverter<Typ> converter = new StringConverter<Typ>() {
        @Override
        public String toString(Typ obj) {
            return obj.getTyp();
        }

        @Override
        public Typ fromString(String string) {
            Typ obj = cell.getItem();
            if (obj == null) {
                Typ newObj = new Typ("");
                newObj.setTyp(string);

                return newObj;
            } else {
                obj.setTyp(string);

                return obj;
            }
        }

    };

    cell.setConverter(converter);

    return cell;
});

Actually with this code, I can commit change with enter key.

Any help please?

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Related/possible duplicate: http://stackoverflow.com/questions/29576577/tableview-doesnt-commit-values-on-focus-lost-event It is unclear _when_ you expect the value to be committed, but assuming you mean on losing focus, see linked question. – Itai Dec 09 '16 at 14:18
  • It works when i test with a TableView but with my ListView it doesn't work – user5772710 Dec 11 '16 at 19:41
  • Please post your `ListView` version of the linked solution, maybe it needs modification, but it's impossible to say without knowing what you tried. – Itai Dec 11 '16 at 21:13

1 Answers1

0

I found the solution for my problem.

here is my code:

  objetListview1.setCellFactory(lv -> new ListCell<Typ>() {
            private TextField textField = new TextField() ;

            {
                textField.setOnAction(e -> {
                    commitEdit(getItem());
                });

               textField.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                if (!newValue) {
                    System.out.println("Commiting " + textField.getText());
                    commitEdit(getItem());
                }
            });
            }

            @Override
            protected void updateItem(Typ person, boolean empty) {
                super.updateItem(person, empty);
                if (empty) {
                    setText(null);
                    setGraphic(null);
                } else if (isEditing()) {
                    textField.setText(person.getTyp());
                    setText(null);
                    setGraphic(textField);
                } else {
                    setText(person.getTyp());
                    setGraphic(null);
                }
            }

            @Override
            public void startEdit() {
                super.startEdit();
                textField.setText(getItem().getTyp());
                setText(null);
                setGraphic(textField);
                textField.selectAll();
                textField.requestFocus();
            }

            @Override
            public void cancelEdit() {
                super.cancelEdit();
                setText(getItem().getTyp());
                setGraphic(null);
            }

            @Override
            public void commitEdit(Typ person) {
                super.commitEdit(person);
                person.setTyp(textField.getText());
                setText(textField.getText());
                setGraphic(null);
            }
        });