使用colorpicker编辑器的JavaFX tableview

我有一个TableView,它使用ColorPicker来(显示/编辑)单元格中的颜色。 该表在所需字段中显示ColorPicker,但编辑不起作用。

TableColumn c2 = new TableColumn("Color"); c2.setCellValueFactory(new PropertyValueFactory("color")); c2.setCellFactory(new Callback<TableColumn, TableCell>() { @Override public TableCell call(final TableColumn param) { TableCell cell = new TableCell() { @Override public void updateItem(Color c, boolean empty) { if(c != null) { final ColorPicker cp = new ColorPicker(); cp.setValue(c); setGraphic(cp); cp.setOnAction(new EventHandler() { public void handle(javafx.event.ActionEvent t) { getTableView().edit(getTableRow().getIndex(), param); commitEdit(cp.getValue()); } }); } } }; return cell; } }); c2.setOnEditCommit(new EventHandler<CellEditEvent>() { @Override public void handle(CellEditEvent t) { ((SeriesPreferences) t.getTableView().getItems().get(t.getTablePosition(). getRow())).setColor(t.getNewValue()); } }); 

当我更改颜色选择器中的颜色时,没有调用编辑事件处理程序,任何想法?

如果其属性正确绑定到表,则无需直接访问JavaFX POJO(或JavaFX Bean),也无需调用除commitEdit之外的任何内容。

Max Beikirch的答案具有误导性,因为当桌子未处于编辑模式时,它会使颜色选择器(以及颜色)消失。 这是一种将表格置于编辑模式的解决方法,但这是一个糟糕的模式。 所以在点击按钮显示颜色选择器弹出窗口之前这样做:

用这样的颜色选择器写你的单元格:

 public class ColorTableCell extends TableCell { private final ColorPicker colorPicker; public ColorTableCell(TableColumn column) { this.colorPicker = new ColorPicker(); this.colorPicker.editableProperty().bind(column.editableProperty()); this.colorPicker.disableProperty().bind(column.editableProperty().not()); this.colorPicker.setOnShowing(event -> { final TableView tableView = getTableView(); tableView.getSelectionModel().select(getTableRow().getIndex()); tableView.edit(tableView.getSelectionModel().getSelectedIndex(), column); }); this.colorPicker.valueProperty().addListener((observable, oldValue, newValue) -> { if(isEditing()) { commitEdit(newValue); } }); setContentDisplay(ContentDisplay.GRAPHIC_ONLY); } @Override protected void updateItem(Color item, boolean empty) { super.updateItem(item, empty); setText(null); if(empty) { setGraphic(null); } else { this.colorPicker.setValue(item); this.setGraphic(this.colorPicker); } } } 

如果您使用的是Java 7,请将lambdas替换为匿名内部类,但它也可以正常工作。 完整的博客文章在这里 。

好吧,我调查了这个话题,因为我遇到了同样的问题。 我不敢说JavaFX只是无法使用。

我看了一下其他人是如何实现他们的单元格的,关键是那些都是使用可以用字符串表示的东西。 现在,它始终与Java一样:用Java方式做,或者在雨中独处。 JavaFX的文档目前非常糟糕,所以我必须尝试直到它工作。

所以:要触发editCommit -event,你必须在updateItem()调用setContentDisplay(ContentDisplay. TEXT_ONLY) updateItem() 。 如果想要将数据显示为字符串,那么效果很好,但在像这样的情况下完全失败,其中colorpicker只是完成工作。

或者,也可以手动触发事件。 但是你怎么得到桌位呢? 我不知道。

我有同样的问题CheckBoxTableCell和DatePickerTableCell和ColorPickerTableCells 🙁

我这样处理它:在控件的事件中,我通过“ ((输入)getTableView()。getItems()。get(getTableRow()。getIndex() ”返回使用的POJO对象,我更新类似于它是在OnEditCommit方法中完成的……

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

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

以下是ColorPickerCell的示例:

 public class ColorPickerTableCell extends TableCell{ private ColorPicker cp; public ColorPickerTableCell(){ cp = new ColorPicker(); cp.setOnAction(new EventHandler() { @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 = new SimpleObjectProperty(); this.color = new SimpleObjectProperty(color); public ObjectProperty colorProperty() { return color; } public void setColor(Color color2) { color.set(color2); } 

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

问候,

就像Michael Simons在对OP的评论中所说的那样。 您需要处于编辑模式 。 创建自己的自定义单元格时,可以通过调用startEdit();手动触发编辑模式startEdit(); 来自TableCell内部。

例如,使用控件的focusProperty:

  cp.focusedProperty().addListener((observable, oldValue, newValue) -> { if (newValue) { startEdit(); } });