How to put ComboBoxTableCell in a TableView?

user3292323 picture user3292323 · May 8, 2014 · Viewed 14.3k times · Source

I'm trying to put a ComboBox in a table cell, but I can't. the code is the next:

private void cargaTablaDesglose() {
    TableColumn<Map, String> column1 = new TableColumn<>(Desglose1);
    TableColumn<Map, String> column2 = new TableColumn<>(Desglose2);
    TableColumn<Map, String> column3 = new TableColumn<>(Desglose3);

    column1.setCellValueFactory(new MapValueFactory(Desglose1));
    column1.setMaxWidth(0);
    column2.setCellValueFactory(new ComboBoxTableCell.forTableColumn(null));
    column2.setPrefWidth(150);
    column3.setCellValueFactory(new MapValueFactory(Desglose3));
    column3.setPrefWidth(250);

    if (CUOD.modifyData()) {
        column2.setOnEditCommit((TableColumn.CellEditEvent<Map, String> t) -> {
            actualizaObra(t.getRowValue(), t.getNewValue());
        });
            }

    tablaDesglose.getItems().clear();
    tablaDesglose.setEditable(true);
    tablaDesglose.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    tablaDesglose.getSelectionModel().setCellSelectionEnabled(false);
    tablaDesglose.getColumns().clear();
    tablaDesglose.getColumns().addAll(column1, column2, column3);

    Callback<TableColumn<Map, String>, TableCell<Map, String>> cellFactoryMap
            = CUCF.getFactoryMap();
    column1.setCellFactory(cellFactoryMap);
    column2.setCellFactory(cellFactoryMap);
    column3.setCellFactory(cellFactoryMap);
}

It says that can't found forTableColumn method of ComboBoxTableCell

Answer

SimY4 picture SimY4 · May 8, 2014

The part of the problem is that you're trying to set cell factory into cell value factory field of TableColumn. Try this instead:

ObservableList<String> cbValues = FXCollections.observableArrayList("1", "2", "3");

TableColumn<Map, String> column2 = new TableColumn<>(Desglose2);
column2.setCellFactory(ComboBoxTableCell.forTableColumn(new DefaultStringConverter(), cbValues));