my problem is quiet simple. I overrode a few methods of TableCell as follows:
class ChoiceBoxTableCell<S, T> extends TableCell<S, T> {
private ChoiceBox<T> view;
private ObservableList<T> values;
@Override
public void cancelEdit() {
super.cancelEdit();
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
private void createView() {
view = new ChoiceBox<T>(values);
view.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
final EventHandler<KeyEvent> keyHandler = new EventHandler<KeyEvent>() {
@Override
public void handle(final KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(view.getSelectionModel().getSelectedItem());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
};
view.setOnKeyPressed(keyHandler);
view.setOnKeyTyped(keyHandler);
view.setOnKeyReleased(keyHandler);
}
public ObservableList<T> getValues() {
return values;
}
public void setValues(final ObservableList<T> values) {
this.values = values;
}
@Override
public void startEdit() {
super.startEdit();
System.out.println("ChoiceBoxTableCell.startEdit() -> editable: " + isEditable()); // print is true
System.out.println("ChoiceBoxTableCell.startEdit() -> editing: " + isEditing()); // print is false
if (view == null) {
createView();
} else {
view.getSelectionModel().select(getItem());
}
setGraphic(view);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
@Override
public void updateItem(final T item, final boolean empty) {
super.updateItem(item, empty);
if (view != null) {
view.getSelectionModel().select(item);
}
if (item != null) {
setText(item.toString());
}
}
}
Now i would expect that the variable 'editing' would be set to true after running through super.startEdit(). In the following class the code works:
abstract class AbstractEditingTableCell<S, T> extends TableCell<S, T> {
private TextField textField;
public AbstractEditingTableCell() {}
@Override
public void cancelEdit() {
super.cancelEdit();
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
private void commit() {
try {
final String text = textField.getText();
commitEdit(parse(text));
} catch (final Exception e) {
// FIXME Ungültige Eingabe: TextField markieren als fehlerhaft
e.printStackTrace();
}
}
private void createTextField() {
textField = new TextField(getFormatted(getItem()));
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
final EventHandler<KeyEvent> keyHandler = new EventHandler<KeyEvent>() {
@Override
public void handle(final KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commit();
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
};
textField.setOnKeyPressed(keyHandler);
textField.setOnKeyTyped(keyHandler);
textField.setOnKeyReleased(keyHandler);
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(final ObservableValue<? extends Boolean> observable, final Boolean oldValue, final Boolean newValue) {
if (!newValue) {
commit();
}
}
});
}
public abstract String getFormatted(T item);
protected abstract T parse(String value) throws Exception;
@Override
public void startEdit() {
super.startEdit();
System.out.println("AbstractEditingTableCell.startEdit() -> editing: " + isEditing()); // print is true
if (textField == null) {
createTextField();
} else {
textField.setText(getFormatted(getItem()));
}
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
@Override
public void updateItem(final T item, final boolean empty) {
super.updateItem(item, empty);
if (textField != null) {
textField.setText(getFormatted(item));
}
setText(getFormatted(item));
}
}
The only difference I see is the ChoiceBox. Does anyone have a suggestion for my problem?
PS: please excuse my english i'm not a native ;)
thank you for your time
patrick