commitEdit不会在focusedProperty actionListener上调用setOnEditCommit事件处理程序

时间:2015-04-07 15:49:56

标签: javafx focuslistener

我创建了一个带有一个可编辑文本字段的表视图。当我在字段中输入一些文本并按ENTER键时,该值会反映在表中,我的意思是值被提交(setOnEditCommit得到调用)。但是在输入文本后我只是从焦点移动焦点时,值没有反映在表格上(setOnEditCommit未被调用)。

请找到以下代码。

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);
}

自定义文本单元格。

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处理程序按预期工作,但textField.focusedProperty()侦听器无效。

任何人都可以告诉我,如果我的代码有任何问题。感谢

0 个答案:

没有答案