javajavafxtableviewtablecolumn

Editing TableView in JavaFX


Does anyone know how to change the values ​​in TableView? I develop with SceneBuilder, so I am afraid, that any tutorial without SceneBuilder can't help me.

I tried to put code into the initialize method, which worked in program without SceneBuilder. I also tried to set a method "setOnEditCommit " in SceneBuilder, but the method didn't start even after clicking on a row in the TableColumn.

Edit:

I tried this in the method initialize:

@Override
public void initialize(URL url, ResourceBundle rb) {
    ...
    //initialize TableViewu
    javafx.util.Callback<TableColumn, TableCell> cellFactory = new javafx.util.Callback<TableColumn, TableCell>() {
        @Override
        public TableCell call(TableColumn p) {
            return new EditingCell();
        }
    };
    vesniceTableColumn.setCellValueFactory(new PropertyValueFactory<Cil, String>("vesnice"));
    souradniceTableColumn.setCellValueFactory(new PropertyValueFactory<Cil, String>("souradnice"));
    pocetTableColumn.setEditable(true);
    pocetTableColumn.setCellValueFactory(new PropertyValueFactory<Cil, Integer>("pocet"));
    pocetTableColumn.setCellFactory(cellFactory);
    pocetTableColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Cil, Integer>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<Cil, Integer> t) {
            ((Cil) t.getTableView().getItems().get(t.getTablePosition().getRow())).setPocet(t.getNewValue());
        }
    });
    tableView.setItems(cilVesnice);
}

or

@Override
public void initialize(URL url, ResourceBundle rb) {
    ...
    //initialize TableViewu
    vesniceTableColumn.setCellValueFactory(new PropertyValueFactory<Cil, String>("vesnice"));
    souradniceTableColumn.setCellValueFactory(new PropertyValueFactory<Cil, String>("souradnice"));
    pocetTableColumn.setEditable(true);
    pocetTableColumn.setCellValueFactory(new PropertyValueFactory<Cil, Integer>("pocet"));
    pocetTableColumn.setCellFactory(TextFieldTableCell.<Cil, Integer>forTableColumn(new IntegerStringConverter()));
    pocetTableColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Cil, Integer>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<Cil, Integer> t) {
            ((Cil) t.getTableView().getItems().get(t.getTablePosition().getRow())).setPocet(t.getNewValue());
        }
    });
    tableView.setItems(cilVesnice);
}

Solution

  • I found the mistake.

    I forgot to set the TableView as editable. setEditable(true) on the TableView solved the problem.