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

前端 未结 1 1131
悲&欢浪女
悲&欢浪女 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<String> 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<TableColumn<RequirementsProperty, String>, TableCell<RequirementsProperty, String>>() {
             @Override
             public TableCell<RequirementsProperty, String> call(TableColumn<RequirementsProperty, String> param) {
                  ObservableList<String> testlist = FXCollections.observableArrayList("A", "B", "C");
                  return new ChoiceBoxTableCell(testlist);
             }
        });
    
    0 讨论(0)
提交回复
热议问题