我一直在尝试将我的 Product 类中的一行信息添加到这个 tableView 中:
当我点击添加按钮并运行时:
productTableView.getItems().add(new Product(...)
它在我的表中添加了一个空白行:
控制器.java
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())
);
}
Inventory_Management.fxml
<?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 有问题。