JavaFX TableView 列显示错误值

标签 javafx

我想用 JavaFX 制作一个表格,显示“产品”、“价格”和“数量”。但是,在“价格”列中显示的值是“数量”的值,而在“数量”列中则不显示任何内容。

产品类,用于创建具有三个值的产品:名称、价格、数量。

我仍在学习如何使用 JavaFX。此代码来自 youtube 教程视频。我确信我输入的内容与视频完全相同,但我不知道为什么它没有显示与教程相同的表格。

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

我不知道哪一套是错误的。

这是代码。

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;
    }
}

这是产品类别

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;
    }
}

使表格单元格显示正确的值

最佳答案

您调用 priceColum.setCellValueFactory 两次,一次调用价格,然后再次调用数量,因此最后一次调用生效并显示价格的数量。

关于JavaFX TableView 列显示错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76025275/

相关文章:

java - 尝试创建 EXE 文件时出现 "Unable to create javax script engine for javascript"错误

JavaFX - 如何检测 Windows 注销/关闭请求?

Java - 将值从 TextField 添加到另一个类的对象构造函数

JavaFX容器结构滚动面板

java - 在 JavaFX UI 中显示 littleproxy 日志

java - 查看fxml里面的逻辑(特别是迭代)

java - SQLNonTransientConnectionException

java - 在条形图中显示相同的值

java - Jar 找不到 .fxml 文件的路径

internet-explorer - 无法使用 Internet Explorer 第二次(重新启动)启动 JavaFX Web 应用程序