3

have a look the following working code:

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

However, as soon as I am using a custom TableCell, the code in setOnEditCommit is not called anymore:

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

}

}

A slight change of the code above...

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

... makes the code not work anymore. The exception is never thrown. I think that I am doing something wrong in the design of ColorPickerTableCell, but I cannot imagine what. How can I make JavaFX call my OnEditCommit ?

Max Beikirch
  • 2,053
  • 5
  • 25
  • 36
  • probably a duplicate of this here, but that one wasn't solved either: http://stackoverflow.com/questions/12169116/javafx-tableview-with-colorpicker-editor – Max Beikirch Jul 08 '13 at 08:02

4 Answers4

2

You need first to go to edit state with statEdit(); , if you now commit an event will be fired

justcode
  • 21
  • 2
1

I've recently had the same problem. Unfortunately, I haven't found any way of trigerring that event from the ColorPicker control. However, I came up with the following workaround.

First of all, I created a Color wrapper class:

public class ColorWrapper {

 private Color color;

 ....
}

I replaced the Color instance with the wrapper instance in my model class M. Next, I implemented setCellFactory method in the following way:

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

            };

        }            

    });

As you can see, I just made use of setUserData/getUserData methods of the ColorPicker class and that's it. It works.

1

I had the same problem for CheckBoxTableCell and DatePickerTableCell and ColorPickerTableCells :-(

I deal it like that: on the events of the controls I get back the POJO objects in use by the "((Inputs)getTableView().getItems().get(getTableRow().getIndex()" and I update similary like is it done in the OnEditCommit method...

So for me it's look like this (update the color):

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

Here is example with 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);
}
}

With this simple JavaFX's 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);
}

I do not know if it's a good way to achive that but it worked for me... Note that the JavaFX's POJO is only accessible within an "ActionEvent" request (combobox, datepicker, colorpicker, etc..)

Regards,

neuronsoverflow
  • 133
  • 4
  • 14
0

To elaborate on justcode's answer, here is my class where I had the problem and solved it:

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

}
Thomas
  • 624
  • 11
  • 28