I have created a table view with with one Editable text feild.When i enter some text in the feild and press ENTER key, the value gets reflected on the table, i mean the value gets commited(setOnEditCommit
gets invoked). But after entering the text and i just move the focus from the feild the value is not reflected on the table(setOnEditCommit
is not invoked).
Please find the code below.
public class TabViewController {
@FXML
private TabPane cnfmTab;
@FXML
private TableView<TabVO> partsTable;
@FXML
private TableColumn<TabVO, String> column1;
@FXML
private TableColumn<TabVO, String> column2;
@FXML
private TableColumn<TabVO, String> column3;
private ObservableList<TabVO> tableData = FXCollections.observableArrayList();
private ObservableList<String> column1List;
@FXML
public void initialize(){
tableData = FXCollections.observableArrayList(callDAO.getTableData(1));
Callback<TableColumn<TabVo, String>, TableCell<TabVo, String>> cellFactory =
new Callback<TableColumn<TabVo, String>, TableCell<TabVo, String>>() {
public TableCell<TabVo, String> call(TableColumn<TabVo, String> p) {
return new TextEditCell();
}
};
column3.setCellFactory(cellFactory);
column3.setOnEditCommit(
new EventHandler<CellEditEvent<TabVo, String>>() {
@Override
public void handle(CellEditEvent<TabVo, String> t) {
System.out.println("Inside Column3 handler");
((TabVo) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setColumn3(t.getNewValue());
}
});
column3.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCpmComment()));
partsTable.setItems(tableData);
}
Custom Text Cell.
public class TextEditCell extends TableCell<TabVo, String>{
/** The text field. */
private TextField textField;
/**
* Instantiates a new text edit cell.
*/
public TextEditCell() {
}
/* (non-Javadoc)
* @see javafx.scene.control.TableCell#startEdit()
*/
@Override
public void startEdit() {
super.startEdit();
if (!isEmpty()) {
if(textField == null){
createTextField();
}
setText(null);
setGraphic(textField);
textField.requestFocus();
textField.selectAll();
}
}
/* (non-Javadoc)
* @see javafx.scene.control.TableCell#cancelEdit()
*/
@Override
public void cancelEdit() {
super.cancelEdit();
setText((String) getItem());
setGraphic(null);
}
/* (non-Javadoc)
* @see javafx.scene.control.Cell#updateItem(java.lang.Object, boolean)
*/
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
} else {
setText(getString());
setGraphic(null);
}
}
}
/**
* Creates the text field.
*/
private void createTextField() {
textField = new TextField(getItem());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if(event.getCode() == KeyCode.ENTER){
commitEdit(textField.getText());
}
}
});
textField.focusedProperty().addListener(new ChangeListener<Boolean>(){
@Override
public void changed(ObservableValue<? extends Boolean> arg0,
Boolean arg1, Boolean arg2) {
if (!arg2.booleanValue() && textField != null) {
System.out.println("Inside focus property " + textField.getText());
commitEdit(textField.getText());
}
}
});
}
/**
* Gets the string.
*
* @return the string
*/
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
}
textField.setOnKeyPressed
handler is working as expected, but textField.focusedProperty()
listener is not working.
Can anyone please tell, if anything is wrong with my code. Thanks