如何在JavaFX中创建表格单元工厂以显示ChoiceBox? [英] How do you create a table cell factory in JavaFX to display a ChoiceBox?
问题描述
我正在尝试在JavaFX中的TableView中显示ChoiceBox。现在我只是想测试一下我是否可以使用它,所以我在单元工厂内生成假数据,但我甚至无法使其工作。
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 factory, but I can't even get this to work.
我的IDE给了我错误
forTableColumn (javafx.collections.ObservableList<T>) in ChoiceBoxTableCell cannot be applied
to (javafx.collections.ObservableList<java.lang.String>)
这是我的代码。
private ListView<RequirementsProperty> guiPropertyList;
private TableColumn<RequirementsProperty, String> guiSpecifierColumn;
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 ChoiceBoxTableCell.forTableColumn(testlist);
}
});
有谁知道我做错了什么?我现在只想显示包含A,B和C的列表,然后我可以继续显示我自己的数据。
Does anyone know what I am doing wrong? I just want to display the list containing A, B, and C right now, and then I can move on to displaying my own data.
推荐答案
ChoiceBoxTableCell.forTableColumn(...)
返回 Callback
本身(即返回 cellFactory
,而非单元格。
ChoiceBoxTableCell.forTableColumn(...)
returns a Callback
itself (i.e. it returns the cellFactory
, not the cell).
你可以这样做
ObservableList<String> testlist = FXCollections.observableArrayList("A", "B", "C");
guiSpecifierColumn.setCellFactory(ChoiceBoxTableCell.forTableColumn(testlist));
如果您自己实施工厂,其电话(...)
方法需要返回实际的单元格。在这种情况下,您只需直接构造 ChoiceBoxTableCell
并将其返回:
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);
}
});
这篇关于如何在JavaFX中创建表格单元工厂以显示ChoiceBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!