0

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);
}
Michael
  • 11
  • 2
  • Isn't the only problem with the code you commented out that the types are wrong? You declared `tablePrice` as a `TableColumn` but you are returning a `StringProperty`. – James_D Apr 03 '18 at 23:52
  • @James_D no James, I modified back that part for the umcommented code to work – Michael Apr 04 '18 at 00:08
  • You also seem to have `getValue()` twice in the method call chain, which doesn't match how you describe your model classes to be set up. What, exactly, is the error you are getting if you uncomment the commented code? – James_D Apr 04 '18 at 00:11

1 Answers1

0

Declare tablePrice as

@FXML private TableColumn<MenuItem, Number> tablePrice;

(see JavaFX Properties in TableView for why).

Then the cell value factory should return a Property<Number> (yours is returning a Property<String>), so you need

tablePrice.setCellValueFactory(new Callback<CellDataFeatures<MenuItem, Number>, ObservableValue<Number>>() {
    @Override
    public ObservableValue<Number> call( CellDataFeatures<MenuItem, Number> c) {
      return new SimpleDoubleProperty(c.getValue().getCurrentPricedMenuItem().getPrice());
    }
}); 

or, using lambda expressions to get rid of most of the boilerplate code

tablePrice.setCellValueFactory(cellData -> 
    new SimpleDoubleProperty(cellData.getValue().getCurrentPricedMenuItem().getPrice()));

(I'm pretty sure you should not have two getValue() calls in there... but you didn't post your model classes.)

James_D
  • 201,275
  • 16
  • 291
  • 322