Edited values are not saved TableView.Java


Why is the user's edited text not saved in the table after pressing Enter?Colum.IsEditable (true) is also a table.What is the problem?Only the text of the 1st column is saved.

Author: Andrew Relise Auger, 2018-05-24

1 answers

Since you did not provide the code, then no one can help you with your situation. Here is a working example, compare with what you have and look for differences.

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        TableColumn<Person, String> tcSurname = new TableColumn<>("Фамилия");
        tcSurname.setCellValueFactory(param -> param.getValue().surname);
        tcSurname.setCellFactory(TextFieldTableCell.forTableColumn());

        TableColumn<Person, String> tcName = new TableColumn<>("Имя");
        tcName.setCellValueFactory(param -> param.getValue().name);
        tcName.setCellFactory(TextFieldTableCell.forTableColumn());

        TableColumn<Person, String> tcPatronymic = new TableColumn<>("Отчество");
        tcPatronymic.setCellValueFactory(param -> param.getValue().patronymic);
        tcPatronymic.setCellFactory(TextFieldTableCell.forTableColumn());

        TableView<Person> tableView = new TableView<>();
        tableView.getColumns().addAll(tcSurname, tcName, tcPatronymic);
        tableView.setEditable(true);

        tableView.getItems().addAll(
                new Person("Иванов", "Иван", "Иванович"),
                new Person("Петров", "Петр", "Петрович"),
                new Person("Николаев", "Николай", "Николаевич")
        );

        primaryStage.setScene(new Scene(tableView, 300, 300));
        primaryStage.show();
    }

    private static class Person {

        StringProperty surname = new SimpleStringProperty(this, "surname");
        StringProperty name = new SimpleStringProperty(this, "name");
        StringProperty patronymic = new SimpleStringProperty(this, "patronymic");

        public Person(String surname, String name, String patronymic) {
            this.surname.set(surname);
            this.name.set(name);
            this.patronymic.set(patronymic);
        }
    }

}
 0
Author: Александр Савостьянов, 2018-05-28 05:13:57