Self made ChoiceBoxTableCell startEdit-method does

2019-07-08 09:15发布

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

标签: javafx-2
1条回答
不美不萌又怎样
2楼-- · 2019-07-08 09:57

Regarding the original question ("startEdit-method does not turn Cell.editing into true"), one possible problem is discussed in this article, which also provides a solution: http://www.wobblycogs.co.uk/index.php/computing/javafx/145-editing-null-data-values-in-a-cell-with-javafx-2

Also, see here (the "Linked" section is a bit hidden): Dealing with null values in Cell

查看更多
登录 后发表回答