【发布时间】:2016-05-09 22:58:11
【问题描述】:
我正在尝试将Combo Box 添加到我的Table View:
基本上我有一个名为 TableViewTest 的类,它存储名称和描述,我可以在 Table View 中显示这些名称和描述,但我想要做的是添加第三列,每个单元格都有一个 @ 987654324@ 以便用户可以从多个选项中为每个人选择一个。
到目前为止,我已经创建了一个带有一些值的String 类型的ObservableList,并将它们添加到ComboBox 对象中。有谁知道我可以将此Combo Box 添加到表格中吗?
另外请记住,这段代码非常粗糙,我只是想让一些东西正常工作,我会在以后重构代码。
ObservableList<TableViewTest> products = FXCollections.observableArrayList();
for(int i = 0; i < b.length; i++){
// random String values
products.add(new TableViewTest(b[i], a[i]));
}
ObservableList<String> options = FXCollections.observableArrayList(
"1",
"2",
"3"
);
final ComboBox comboBox = new ComboBox(options);
TableColumn<TableViewTest, String> nameColumn = new TableColumn<> ("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<TableViewTest, String>("name"));
//price Column
//Stock Column
TableColumn<TableViewTest, String> StockColumn = new TableColumn<> ("Stock");
StockColumn.setMinWidth(150);
StockColumn.setCellValueFactory(new PropertyValueFactory<TableViewTest, String>("description"));
TableColumn<Object,ComboBox> PriceColumn;
PriceColumn = new TableColumn<>("Source");
PriceColumn.setMinWidth(150);
//PriceColumn.setCellValueFactory(new PropertyValueFactory<>
//(options));
//PriceColumn.setCellFactory(ComboBoxTableCell.forTableColumn(new
//DefaultStringConverter(), options));
//PriceColumn.setCellFactory(ComboBoxTableCell.forTableColumn(
//comboBox));
TableView<TableViewTest> table = new TableView<>();
table.setItems(products);
table.getColumns().addAll(nameColumn, StockColumn, PriceColumn);
【问题讨论】:
-
您的表格列类型应该是该列中显示的数据的类型,而不是用于显示它的控件的类型。 IE。你需要
TableColumn<..., String> priceColumn(我认为应该是TableColumn<TableViewTest, String> priceColumn)。那么你应该可以使用ComboBoxTableCell。 -
我真的希望有一些这样的 FXML 示例。有人知道吗?