How do you create a table cell factory in JavaFX to display a ChoiceBox?

前端 未结 1 1132
悲&欢浪女
悲&欢浪女 2021-01-25 16:29

I am trying to display a ChoiceBox inside of a TableView in JavaFX. Right now I am just trying to test if I can get this working, so I am generating fake data within the cell fa

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-25 16:42

    ChoiceBoxTableCell.forTableColumn(...) returns a Callback itself (i.e. it returns the cellFactory, not the cell).

    You can just do

    ObservableList testlist = FXCollections.observableArrayList("A", "B", "C");
    guiSpecifierColumn.setCellFactory(ChoiceBoxTableCell.forTableColumn(testlist));
    

    If you are implementing the factory yourself, its call(...) method needs to return the actual cell. In this case you would just construct a ChoiceBoxTableCell directly and return it:

    guiSpecifierColumn.setCellFactory(
        new Callback, TableCell>() {
             @Override
             public TableCell call(TableColumn param) {
                  ObservableList testlist = FXCollections.observableArrayList("A", "B", "C");
                  return new ChoiceBoxTableCell(testlist);
             }
        });
    

    0 讨论(0)
提交回复
热议问题