I am trying to find a way to populate my tablePrice
column with prices of specific menu items. The code shown below works fine but I had to create a price variable in the MenuItem
class which was not previously there. MenuItem
and PricedMenuItem
, as well as other classes are generated from a UML domain model for a restaurant management system. This current method is discouraged as I am modifying the domain model.
The commented section shows how far I got with an error on the setCellValueFactory()
. Is there a way for a TableView
to contain columns of different classes? If so, can someone please assist me in filling in the column directly from the PricedMenuItem
class?
MenuItem
has a private name
and itemCategory
enum, as well as a getCurrentPricedMenuItem()
method.
PricedMenuItem
has a private price
as well as a getPrice()
method.
@FXML private TableView<MenuItem> tableView;
@FXML private TableColumn<MenuItem, String> tableName;
@FXML private TableColumn<MenuItem, Double> tablePrice;
@FXML private TableColumn<MenuItem, ItemCategory> tableCategory;
@Override
public void initialize(URL location, ResourceBundle resources) {
tableName.setCellValueFactory(new PropertyValueFactory<MenuItem, String>("Name"));
tableCategory.setCellValueFactory(new PropertyValueFactory<MenuItem, ItemCategory>("itemCategory"));
tablePrice.setCellValueFactory(new PropertyValueFactory<MenuItem, Double>("price"));
/*way to retrieve price directly from PMI
tablePrice.setCellValueFactory(new Callback<CellDataFeatures<MenuItem, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call( CellDataFeatures<MenuItem, String> c) {
return new SimpleStringProperty(c.getValue().getValue().getCurrentPricedMenuItem().getPrice()+"");
}
});
*/
categoryDropDown1.getItems().setAll(ItemCategory.values());
categoryDropDown2.getItems().setAll(ItemCategory.values());
tableView.setItems(loadCurrentMenuItems());
updateBox("Select a menu item to edit.", Color.BLACK);
}