Reputation: 1293
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);
}
Upvotes: 1
Views: 2377
Reputation: 1293
I found the mistake.
I forgot to set the TableView as editable. setEditable(true)
on the TableView solved the problem.
Upvotes: 1
Reputation: 1724
You need to create a controller class. In the scene builder, you need to click controller in the scene builder (bottom left) and then type the name of the controller class you will make. Then, click "View" and then click "Sample controller skeleton." Copy this into a java class of the same name as the one you set in the controller pane.
Upvotes: 0