How to put ComboBoxTableCell in a TableView?

淺唱寂寞╮ 提交于 2019-12-19 03:42:10

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/23537358/how-to-put-comboboxtablecell-in-a-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!