我一直试图从我的Product类中向该tableView添加一行信息:
当我点击添加按钮并运行时:
productTableView.getItems().add(new Product(...)
它将在我的表中添加空白行:
import javafx.scene.control.TableView;
public class Controller {
// Initialized all elements of GUI
public TableView<Product> productTableView;
public TableColumn<Product, Integer> productID;
public TableColumn<Product, String> name;
public TableColumn<Product, Double> price;
public TableColumn<Product, Integer> quantity;
public TableColumn<Product, Double> cost;
public TableColumn<Product, Date> bought;
public TableColumn<Product, Date> sold;
public TableColumn<Product, Integer> threshold;
public void addProduct() {
productTableView.getItems().add(new Product( 1, "Toilet Paper", 9.99, 1.25, 300, 300, new Date(), new Date())
);
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<VBox alignment="TOP_RIGHT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Inventory_Management.Controller">
<children>
<TableView fx:id="productTableView" VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="productID" prefWidth="75.0" text="Product ID">
<cellValueFactory>
<PropertyValueFactory property="productID" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="name" prefWidth="75.0" text="Name">
<cellValueFactory>
<PropertyValueFactory property="name" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="price" prefWidth="75.0" text="Price">
<cellValueFactory>
<PropertyValueFactory property="price" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="quantity" prefWidth="75.0" text="Quantity">
<cellValueFactory>
<PropertyValueFactory property="quantity" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="cost" prefWidth="75.0" text="Cost">
<cellValueFactory>
<PropertyValueFactory property="cost" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="bought" prefWidth="75.0" text="Bought">
<cellValueFactory>
<PropertyValueFactory property="bought" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="sold" prefWidth="75.0" text="Sold">
<cellValueFactory>
<PropertyValueFactory property="sold" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="threshold" prefWidth="75.0" text="Threshold">
<cellValueFactory>
<PropertyValueFactory property="threshold" />
</cellValueFactory>
</TableColumn>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
<padding>
<Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
</padding>
</VBox>
我使用Scene Builder构建了FXML。我不确定如何在不以编程方式创建表的情况下实现行。我输入的数据应该出现在表中,但是我不确定是否是因为我实现行的方式,或者我的PropertyValues是否有问题。
答案 0 :(得分:0)
问题是在我的Product类中,我没有合适的吸气剂和吸气剂。
在这种情况下,我的吸气剂区分大小写。
“错误代码”示例:
public class Product {
private int productID;
public int getID() {
return productID;
}
}
工作代码示例:
public int getProductID() {
return productID;
}
差异很小,但很重要。