I have a TableView, which has few editable columns. In JavaFX Scene Builder for editable table column's on Edit Commit i have mapped a FXML controller method, which calls DAO service to return data from the database. Problem is event handler method is not called, after editing table cell. I want this event to be fired when i hit Tab key after editing cell data. How to do this? Please suggest
3 Answers
I had the same problem for CheckBoxTableCell and DatePickerTableCell and ColorPickerTableCells :-(
I deal it like that: on the events of the controls I get back the POJO objects in use by the "((Inputs)getTableView().getItems().get(getTableRow().getIndex()" and I update similary like is it done in the OnEditCommit method...
So for me it's look like this (update the color):
((Inputs) getTableView().getItems().get(
getTableRow().getIndex())
).setColor(cp.getValue());
Here is example with ColorPickerCell :
public class ColorPickerTableCell<Inputs> extends TableCell<Inputs, Color>{
private ColorPicker cp;
public ColorPickerTableCell(){
cp = new ColorPicker();
cp.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
commitEdit(cp.getValue());
updateItem(cp.getValue(), isEmpty());
((Inputs) getTableView().getItems().get(
getTableRow().getIndex())
).setColor(cp.getValue());
}
});
setGraphic(cp);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setEditable(true);
}
@Override
protected void updateItem(Color item, boolean empty) {
super.updateItem(item, empty);
cp.setVisible(!empty);
this.setItem(item);
cp.setValue(item);
}
}
With this simple JavaFX's POJO:
public ObjectProperty<Color> color = new SimpleObjectProperty<Color>();
this.color = new SimpleObjectProperty(color);
public ObjectProperty<Color> colorProperty() {
return color;
}
public void setColor(Color color2) {
color.set(color2);
}
I do not know if it's a good way to achive that but it worked for me... Note that the JavaFX's POJO is only accessible within an "ActionEvent" request (combobox, datepicker, colorpicker, etc..)
Regards,
Here is what I use to call my DAO from an editable cell of a tableview.
private TableColumn<Person, String> createNameCol(){
TableColumn col = new TableColumn("Name");
col.setCellValueFactory(
new PropertyValueFactory<Person, String>("name"));
col.setCellFactory(TextFieldTableCell.forTableColumn());
col.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<Person, String> t) {
Person p = t.getRowValue();
p.setName(t.getNewValue());
sl.update(p); // This is where I call the update method from my DAO.
}
});
return col;
}
If this doesn't work, please post your code.
EDIT:
Here is a good tutorial for the editable tableViews
Having wasted the best part of a day on this, scouring the internet for examples which all say pretty much the same thing and are all doing what I am already doing, this is what I have discovered:
On editing a value in a cell which is a TextFieldTableCell you have to press the enter key in order for the edit commit to happen. If you tab out of the cell then the cell is still in edit mode (you can keep on pressing tab to get back into the text field of the cell, and if you simple click away from the cell then the cancelEdit method of TextFieldTableCell is invoked thereby cancelling the edit :-(