commit edit不会调用setOneditCommit

时间:2015-08-03 13:12:20

标签: combobox javafx tableview

我的项目中有1个问题 我想要一个用comboBox构建的tableColum,为此我使用:

tableColumn.setCellFactory(ComboBoxTableCell.forTableColumn(cbValues));

它有效,但像DOC说的那样:

  

默认情况下,ComboBoxTableCell在未编辑时呈现为Label,在编辑模式下呈现为ComboBox。

但我想每次都看到comboBox,所以我构建了这段代码

tableColumn.setCellFactory(new Callback<TableColumn<Ambientale, Integer>, TableCell<Ambientale, Integer>>() {

        @Override
        public TableCell<Ambientale, Integer> call(TableColumn<Ambientale, Integer> param) {
            return new ComboBoxCell(cbValues);
        }
    });

我的ComboBoxCell是:

class ComboBoxCell extends TableCell<Ambientale, Integer> implements Callback<Object, Object>{
 private ComboBox combo;


public ComboBoxCell() {
    combo = new ComboBox();
}

public ComboBoxCell(ObservableList items) {
    combo = new ComboBox();
    combo.setItems(items);
    combo.setOnAction(new EventHandler() {

        @Override
        public void handle(Event event) {
            System.out.println(" it's works");
            commitEdit((Integer)combo.getSelectionModel().getSelectedItem()); 
        }
    });
}

@Override
public void updateItem(Integer item, boolean empty) {
    super.updateItem(item, empty);

    if (empty) {
        setText(null);
        setGraphic(null);
    } else {
        combo.getSelectionModel().select(item);
        setGraphic(combo);
        setContentDisplay(ContentDisplay.CENTER);

    }
}

@Override
public Object call(Object param) {
    return new ComboBoxCell();
} 
}
问题出在这里!

class ComboBoxCell extends TableCell<Ambientale, Integer> implements Callback<Object, Object>{
 private ComboBox combo;


public ComboBoxCell() {
    combo = new ComboBox();
}

public ComboBoxCell(ObservableList items) {
    combo = new ComboBox();
    combo.setItems(items);
    combo.setOnAction(new EventHandler() {

        @Override
        public void handle(Event event) {
            System.out.println(" it's works");
            commitEdit((Integer)combo.getSelectionModel().getSelectedItem()); 
        }
    });
}

@Override
public void updateItem(Integer item, boolean empty) {
    super.updateItem(item, empty);

    if (empty) {
        setText(null);
        setGraphic(null);
    } else {
        combo.getSelectionModel().select(item);
        setGraphic(combo);
        setContentDisplay(ContentDisplay.CENTER);

    }
}

@Override
public Object call(Object param) {
    return new ComboBoxCell();
}

} commitEdit(...)不会调用:

tableColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Ambientale, Integer>>() {

        @Override
        public void handle(TableColumn.CellEditEvent<Ambientale, Integer> t) { //aggiornare database cella singola
            System.out.println("not called");
            //other stuffs
        }
    });
你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

它有效,我不知道为什么但它有效。
我在我的ComboBoxCell上添加了这个

combo.focusedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    if (newValue) {
                        getTableView().edit(getIndex(), getTableColumn());
                    } else {
                        commitEdit((Integer)combo.getSelectionModel().getSelectedItem());
                    }
                }
    });