我的问题很简单。我按如下方式覆盖了TableCell的一些方法:
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());
}
}
}
现在我希望在运行super.startEdit()之后将变量'editing'设置为true。在下面的类中,代码可以工作:
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));
}
}
我看到的唯一区别是ChoiceBox。 有没有人对我的问题有任何建议?
PS:请原谅我的英语我不是本地人;)
谢谢你的时间帕特里克
答案 0 :(得分:2)
关于原始问题(“startEdit-method没有将Cell.editing变为true”),本文讨论了一个可能的问题,它也提供了一个解决方案: http://www.wobblycogs.co.uk/index.php/computing/javafx/145-editing-null-data-values-in-a-cell-with-javafx-2
另外,请看这里(“链接”部分有点隐藏): Dealing with null values in Cell