动态地向选定的javafx ComboBoxTableCell添加值 [英] Adding values to selected javafx ComboBoxTableCell dynamically
问题描述
我有一个 TableView
,其中有两列名为Product和Brand。产品可以是不同的品牌。例如。电视有不同的品牌,如三星,索尼等。我在ComboBoxes展示产品品牌。
I have a TableView
with two columns named Product and Brand. A Product can be of different Brands. Eg. TV has different brands like Samsung, Sony etc. I am showing Brands of a Product in ComboBoxes.
这就是我为我的品牌栏添加 ComboBoxTableCell
的方法。
This is how i am adding a ComboBoxTableCell
for my Brand Column.
ObservableList<String> catList = FXCollections.observableArrayList();
categoryCol.setCellFactory(t -> {
ComboBoxTableCell comboCell = new ComboBoxTableCell(catList);
return comboCell;
});
contactTable.getColumns().add(categoryCol);
现在在这些组合框中我想添加品牌值。由于会有不同的产品,所以他们的品牌也会有所不同。例如。
Now in these ComboBoxes i want to add values of Brands. Since There will be different Products so their Brands will be different also. Eg.
Product | Model
----------------------------------------------
TV | ComboBox[Samsung, Sony, Panasonic]
Monitor | ComboBox[Dell, Microsoft, Apple ]
既然ComboBox拥有相同的数据模型(ObservableList)怎么能我通过选择表中的项目为它们添加不同的值。有可能吗?在此先感谢您的帮助。
Now since the ComboBoxes have same data model (ObservableList) how can i add different values to them by selecting the items in table. Is it possible to do ? Thanks in advance for any help.
推荐答案
首先,您需要一个自定义行类来存储元素,
然后你必须 @Override
来自 ComboBoxTreeTableCell $的
startEdit()
c $ c>例如这样:
First of all you need a custom row class in which you store the elements,
then you have to @Override
the startEdit()
from theComboBoxTreeTableCell
for example this way :
@Override public void startEdit() {
MyCustomRow currentRow = getTableRow().getItem();
getItems().setAll(currentRow.getModels());
super.startEdit();
}
}
MyCustomRow:
MyCustomRow:
package mypackage;
import javafx.beans.property.SimpleStringProperty;
import java.util.List;
import java.util.Map;
public class MyCustomRow {
private SimpleStringProperty product;
private SimpleStringProperty model;
private List<String> allModels;
public MyCustomRow(
String product,
String model,
List<String> models) {
this.product = new SimpleStringProperty(product);
this.model = new SimpleStringProperty(product);
this.allModels = models;
}
public String getProduct() {
return product.get();
}
public SimpleStringProperty productProperty() {
return product;
}
public String getModel() {
return model.get();
}
public SimpleStringProperty modelProperty() {
return model;
}
public List<String> getModels() {
return allModels;
}
}
然后在你的控制器类中你可以说:
Then in your contoller class you can say:
ObservableList<String> carList = FXCollections.observableArrayList();
categoryCol.setCellFactory(t -> new ComboBoxTableCell(carList){
@Override public void startEdit() {
MyCustomRow currentRow = getTableRow().getItem();
getItems().setAll(currentRow.getModels());
super.startEdit();
}
});
categoryCol.setCellValueFactory(v -> v.getValue().modelProperty());
contactTable.getColumns().add(categoryCol);
因此每行添加适当的模型。因此,在 ComboBox
中,您将只拥有属于该产品的那些项目(模型)
So each row you add the appropriate models. So in the ComboBox
you will have only those items(models) which belong to the product
这篇关于动态地向选定的javafx ComboBoxTableCell添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!