我有一个控制器,它有一个如下定义的组合框:
private ComboBox warehouseComboBox;
以及在该控制器中填充组合框的方法
@FXML
void createWarehouseInstance (ActionEvent event){
ArrayList<BikePart> inventory = new ArrayList<>(); //empty inventory
String whName = warehouseNameField.getText();
warehouse wh = new warehouse(whName, inventory); //create new instance
warehouseComboBox.getItems().add(wh);
warehouseArrayList.add(wh); //add instance to arrayList
warehouseNameField.clear(); //clear text box
}
我要做的是获取该组合框的状态,并确定我将操作的类的哪个实例。
我的尝试是这种方法。我根据使用组合框选择的实例显示自行车零件列表。
@FXML
void showParts(){
bikePartList.clear();
bikePartList.appendText("Name\tNumber\tListPrice\tSalePrice\tQuantity\tOnSale?\n");
Object selection = warehouseComboBox.getSelectionModel().getSelectedItem();
if (warehouseArrayList.contains(selection)){
bikePartList.appendText(warehouse.printAll()); //what do I do here?
}
}
对于上下文,仓库持有BikeParts,我正在弄清楚如果我想动态创建仓库,我就不能让这个静态
答案 0 :(得分:0)
如果我理解你要做的事情:
首先使用正确的类型声明您的ComboBox
:
private ComboBox<Warehouse> warehouseComboBox;
然后你可以做
@FXML
void showParts(){
bikePartList.clear();
bikePartList.appendText("Name\tNumber\tListPrice\tSalePrice\tQuantity\tOnSale?\n");
Warehouse selectedWarehouse = warehouseComboBox.getSelectionModel().getSelectedItem();
// I think this is what you're trying to do:
bikePartList.appendText(selectedWarehouse.printAll()); //what do I do here?
}
请注意,我更改了您的班级名称以符合standard naming conventions。