使用自定义cellfactory不调用javafx tableview oneditcommit?

时间:2013-07-04 14:51:17

标签: java events javafx tableview tablecell

查看以下工作代码:

    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

4 个答案:

答案 0 :(得分:2)

首先需要使用statEdit()来编辑状态; ,如果你现在提交一个事件将被解雇

答案 1 :(得分:1)

我最近遇到了同样的问题。不幸的是,我还没有找到任何方法来从ColorPicker控件中触发该事件。但是,我提出了以下解决方法。

首先,我创建了一个Color包装类:

public class ColorWrapper {

 private Color color;

 ....
}

我用我的模型类Color中的包装器实例替换了M实例。接下来,我通过以下方式实现了setCellFactory方法:

myColumn.setCellFactory(new Callback<TableColumn<M, ColorWrapper>, TableCell<M, ColorWrapper>>() {          

        @Override
        public TableCell<M, ColorWrapper> call(TableColumn<M,ColorWrapper> arg0) {
            return new TableCell<M, ColorWrapper>(){

                private ColorPicker colorPicker;

                private ColorPicker createPicker(){
                    colorPicker = new ColorPicker();

                    colorPicker.setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent evt) {
                            ColorPicker cp = (ColorPicker)evt.getSource();
                            ColorWrapper cw = (ColorWrapper)cp.getUserData();
                            cw.setColor(cp.getValue());
                        }

                    });
                    return colorPicker;
                }


                @Override
                protected void updateItem(ColorWrapper value, boolean empty) {                      
                    super.updateItem(value, empty);
                    if(empty){
                        return;
                    }

                    if(colorPicker == null){
                        colorPicker = createPicker();
                        colorPicker.setUserData(value);
                    }

                    colorPicker.setValue(value.getColor());
                    setGraphic(colorPicker);
                }

            };

        }            

    });

正如您所看到的,我只是使用了setUserData/getUserData类的ColorPicker方法,就是这样。它有效。

答案 2 :(得分:1)

我对CheckBoxTableCell和DatePickerTableCell以及ColorPickerTableCells有同样的问题: - (

我这样处理:在控件的事件中,我通过“((输入)getTableView()。getItems()获取POJO对象.get(getTableRow()。getIndex() “我更新类似于OnEditCommit方法...

所以对我而言,它看起来像这样(更新颜色):

 ((Inputs) getTableView().getItems().get(
                    getTableRow().getIndex())
                    ).setColor(cp.getValue());

以下是ColorPickerCell的示例 :

public class ColorPickerTableCell<Inputs> extends TableCell<Inputs, Color>{
private ColorPicker cp;

public ColorPickerTableCell(){        
    cp = new ColorPicker(); 
    cp.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            commitEdit(cp.getValue());
            updateItem(cp.getValue(), isEmpty());
            ((Inputs) getTableView().getItems().get(
                    getTableRow().getIndex())
                    ).setColor(cp.getValue());
        }            
    });                
    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);
    cp.setValue(item);
}
}

使用这个简单的JavaFX的POJO:

    public ObjectProperty<Color> color = new SimpleObjectProperty<Color>();

    this.color = new SimpleObjectProperty(color);

    public ObjectProperty<Color> colorProperty() {
    return color;
 }

public void setColor(Color color2) {
    color.set(color2);
}

我不知道这是否是一个很好的方法来实现,但它对我有用...请注意,JavaFX的POJO只能在“ActionEvent”请求(组合框,日期选择器,颜色选择器等)中访问。

此致

答案 3 :(得分:0)

详细说明justcode的答案,这是我的课程,我遇到了问题并解决了它:

public class DeleteButtonCell extends TableCell<Menu, Menu> {

  private Button deleteButton;

  public DeleteButtonCell() {
    deleteButton = new Button();
    deleteButton.setId("trash-button");
    deleteButton.setOnAction((e) -> {
      startEdit();
      commitEdit((Menu) this.getTableView().getItems().get(this.getIndex()));
    });
  }

  @Override
  protected void updateItem(Menu t, boolean empty) {
    super.updateItem(t, empty);
    if (empty) {
      setGraphic(null);
    } else {
      setGraphic(deleteButton);
    }
  }

}