看看下面的工作代码:
class MyType{
SimpleStringProperty myname;
SimpleObjectProperty<Color> mycolor;
}
TableColumn col;
arr = FXCollections.observableArrayList(new ArrayList<MyType>());
tblColName.setCellValueFactory(new PropertyValueFactory("myname"));
// Use the cell-factory provided by TextFieldTableCell.
tblColName.setCellFactory(TextFieldTableCell.forTableColumn());
tblColName.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
@Override
public void handle(TableColumn.CellEditEvent cellEditEvent) {
((MyType) cellEditEvent.getRowValue()).myname.set((String) cellEditEvent.getNewValue());
}
});
但是,一旦我使用自定义 TableCell,setOnEditCommit
就不再调用其中的代码:
public class ColorPickerTableCell<S> extends TableCell<S, Color>{
private ColorPicker cp;
public ColorPickerTableCell(){
cp = new ColorPicker(Color.BLACK);
cp.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
commitEdit(cp.getValue());
updateItem(cp.getValue(), isEmpty());
}
});
setGraphic(cp);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setEditable(true);
}
@Override
protected void updateItem(Color item, boolean empty) {
super.updateItem(item, empty);
cp.setVisible(!empty);
this.setItem(item);
}
public static <T> Callback<TableColumn<Color, T>, TableCell<Color, T>> forTableColumn(){
return new Callback<TableColumn<Color, T>, TableCell<Color, T>>() {
@Override
public TableCell<Color, T> call(TableColumn<Color, T> colorTTableColumn) {
return new ColorPickerTableCell();
}
};
}
}
对上面的代码稍作改动...
TableColumn col;
arr = FXCollections.observableArrayList(new ArrayList<MyType>());
tblColName.setCellValueFactory(new PropertyValueFactory("myname"));
// Use the cell-factory provided by TextFieldTableCell.
tblColName.setCellFactory(ColorPickerTableCell.forTableColumn());
tblColName.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
@Override
public void handle(TableColumn.CellEditEvent cellEditEvent) {
throw new NotImplementedException(); // is never thrown.
}
});
...使代码不再工作。永远不会抛出异常。我认为我在设计中做错了什么ColorPickerTableCell
,但我无法想象是什么。如何让 JavaFX 调用我的OnEditCommit
?