2

I want to make a table with JavaFX that shows "Product", "Price" and "Quantity". Howevey, in the Price column that shows the values which are Quantity's value, and in the Quantity column it shows nothing.

Product Class that is for create products that takes three values, name, price, quantity.

I am still learning how to use JavaFX. This code is from youtube tutorial video. I am sure I have type the extaclly same with the video but I don't know why it doesn't show the same table with the tutorial.

Here the Price column is showing the quantity value. It should not like that.

I don't know which set is wrong.

Here is the code.

public class demo16TableView extends Application {
    Stage window;
    Scene scene;
    TableView<Product> table;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        window = stage;
        window.setTitle("Table view");

        //Set up name colum
        TableColumn<Product, String> nameColum = new TableColumn<>("Product Name");
        nameColum.setMinWidth(200);
        nameColum.setCellValueFactory(new PropertyValueFactory<>("name"));

        //Set up price colum
        TableColumn<Product, Double> priceColum = new TableColumn<>("Price");
        priceColum.setMinWidth(100);
        priceColum.setCellValueFactory(new PropertyValueFactory<>("price"));

        //Set up quantity colum
        TableColumn<Product, Integer> quantityColum = new TableColumn<>("Quantity");
        quantityColum.setMinWidth(100);
        priceColum.setCellValueFactory(new PropertyValueFactory<>("quantity"));

        //Add to table
        table = new TableView<>();
        table.setItems(getProducts());
        table.getColumns().add(nameColum);
        table.getColumns().add(priceColum);
        table.getColumns().add(quantityColum);



        VBox layout = new VBox();
        //layout.setAlignment(Pos.CENTER);
        layout.getChildren().addAll(table);

        scene = new Scene(layout);
        window.setScene(scene);
        window.show();

    }

    public ObservableList<Product> getProducts(){
        ObservableList<Product> products = FXCollections.observableArrayList();
        products.add(new Product("Macbook Pro", 1799.50, 50));
        products.add(new Product("Apple Watch", 349.99, 60));
        products.add(new Product("iPad", 499.00, 70));
        products.add(new Product("iPhone", 999.00, 80));
        products.add(new Product("Sony HeadPhone", 499.00, 90));
        return products;
    }
}

And here is the Product Class

public class Product {
    private String name;
    private double price;
    private int quantity;

    public Product(){};

    public Product(String name, double price, int quantity) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

Make table cells shows the correct value

Slaw
  • 37,820
  • 8
  • 53
  • 80
Steve1132
  • 23
  • 2
  • I do not see anything wrong. So maybe a clean full build will solve it. The ValueProperty might still works on a wrong, older .class file. – Joop Eggen Apr 16 '23 at 00:10
  • 1
    Related: see https://stackoverflow.com/questions/72437983/why-should-i-avoid-using-propertyvaluefactory-in-javafx – James_D Apr 16 '23 at 03:36

1 Answers1

3

You call priceColum.setCellValueFactory twice, once for the price, then again for the quantity, so the last call takes effect and shows the quantity for the price.

jewelsea
  • 150,031
  • 14
  • 366
  • 406