gpt4 book ai didi

java - 如何在JavaFX中显示表信息?

转载 作者:行者123 更新时间:2023-12-01 22:47:07 28 4
gpt4 key购买 nike

以下代码应该显示库存所有零件可观察列表中的零件。我不确定这是否是库存类没有返回正确的对象,或者是否发生了其他事情,或者我在表的初始化中是否遗漏了某些内容。

编辑:我期望 MainViewController initializer 加载 PartIdPartName PartStockPartPrice 通过 Inventory 类通过 observableList allParts 实现。 InHouse 类扩展了 Part 类

MainViewController

package ViewController;

import Model.InHouse;
import Model.Inventory;
import Model.Part;
import Model.Product;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class MainViewController implements Initializable {


private static Inventory inventory;

//Part FXIDs
@FXML private TableView<Part> PartsTable;
@FXML private TableColumn<Part, Integer> PartId;
@FXML private TableColumn<Part, String> PartName;
@FXML private TableColumn<Part, Integer> PartStock;
@FXML private TableColumn<Part, Double> PartPrice;

//sets up the table view and loads the parts from the inventory list
public void initialize(URL url, ResourceBundle rb) {

PartId.setCellValueFactory(new PropertyValueFactory<>("PartId"));
PartName.setCellValueFactory(new PropertyValueFactory<>("PartName"));
PartStock.setCellValueFactory(new PropertyValueFactory<>("PartStock"));
PartPrice.setCellValueFactory(new PropertyValueFactory<>("PartPrice"));

inventory.addPart(new InHouse(0, "name", 5.00, 1, 1, 5, 0));

PartsTable.setItems(inventory.getAllParts());
}

}

Inventory

package Model;

public class Inventory {

public final static ObservableList<Part> allParts = FXCollections.observableArrayList();

public void addPart(Part newPart){
this.allParts.add(newPart);
}

public ObservableList<Part> getAllParts(){
return this.allParts;
}
}

adding parts class

public abstract class Part {

private int id,
stock,
min,
max;
private String name;
private double price;

public Part(int id, String name, double price, int stock, int min, int max) {
this.id = id;
this.stock = stock;
this.min = min;
this.max = max;
this.name = name;
this.price = price;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getStock() {
return stock;
}

public void setStock(int stock) {
this.stock = stock;
}

public int getMin() {
return min;
}

public void setMin(int min) {
this.min = min;
}

public int getMax() {
return max;
}

public void setMax(int max) {
this.max = max;
}

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


}

error output

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
file:/home/Projects/2019.08.03_C482/InventorySystem/dist/run1962514670/InventorySystem.jar!/ViewController/MainView.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at c482.C482.start(C482.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$10(GtkApplication.java:245)
... 1 more
Caused by: java.lang.NullPointerException
at ViewController.MainViewController.initialize(MainViewController.java:142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
Exception running application c482.C482
Java Result: 1

最佳答案

请阅读类 PropertyValueFactoryjavadoc .
这行代码(来自 MainViewController 类)...

PartId.setCellValueFactory(new PropertyValueFactory<>("PartId"));

意味着您的类Part具有以下声明的方法...

public ObservableValue<Integer> PartIdProperty()

请注意,名称区分大小写。换句话说,partIdProperty正确的。这同样适用于彼此的TableColumn。从您发布的代码来看,您的 Part 类中似乎缺少这些方法。

或者,您可以编写自己的接口(interface) Callback 实现。该接口(interface)中方法的声明可能看起来很吓人,但实现起来实际上并不困难。请记住,接口(interface)方法是由JavaFX基础设施调用的。使用作为 Part 类实例的参数调用您的方法,并且您的方法需要返回适当的 ObservableValue您从方法参数中提取的内容。

这是一个使用 Part 类显示零件表的小示例程序。请注意,它使用 method references Java 1.8 中添加的。另请注意,在下面的代码中,该方法 getValue() CellDataFeatures 类返回一个 Part 对象。

import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class CellValueFactoryExample extends Application {
private ObservableList<Part> items;

@Override
public void init() {
items = FXCollections.observableArrayList();
for (int i = 0; i < 10; i++) {
Part part = new Part(i, String.valueOf(i), (double) i, i, i, i);
items.add(part);
}
}

@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Inventory");
TableView<Part> table = createTable();
BorderPane root = new BorderPane();
root.setCenter(table);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}

private TableView<Part> createTable() {
TableView<Part> table = new TableView<Part>(items);

TableColumn<Part, Number> idCol = new TableColumn<>("ID");
idCol.setCellValueFactory(this::getIdColumnValue);

TableColumn<Part, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(this::getNameColumnValue);

TableColumn<Part, Number> priceCol = new TableColumn<>("Price");
priceCol.setCellValueFactory(this::getPriceColumnValue);

TableColumn<Part, Number> stockCol = new TableColumn<>("Stock");
stockCol.setCellValueFactory(this::getStockColumnValue);

TableColumn<Part, Number> minCol = new TableColumn<>("Min");
minCol.setCellValueFactory(this::getMinColumnValue);

TableColumn<Part, Number> maxCol = new TableColumn<>("Max");
maxCol.setCellValueFactory(this::getMaxColumnValue);

table.getColumns().addAll(idCol, nameCol, priceCol, stockCol, minCol, maxCol);
return table;
}

private ObservableValue<Number> getIdColumnValue(CellDataFeatures<Part, Number> param) {
return new SimpleIntegerProperty(param.getValue().getId());
}

private ObservableValue<String> getNameColumnValue(CellDataFeatures<Part, String> param) {
return new SimpleStringProperty(param.getValue().getName());
}

private ObservableValue<Number> getPriceColumnValue(CellDataFeatures<Part, Number> param) {
return new SimpleDoubleProperty(param.getValue().getPrice());
}

private ObservableValue<Number> getStockColumnValue(CellDataFeatures<Part, Number> param) {
return new SimpleIntegerProperty(param.getValue().getStock());
}

private ObservableValue<Number> getMinColumnValue(CellDataFeatures<Part, Number> param) {
return new SimpleIntegerProperty(param.getValue().getMin());
}

private ObservableValue<Number> getMaxColumnValue(CellDataFeatures<Part, Number> param) {
return new SimpleIntegerProperty(param.getValue().getMax());
}

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

这是正在运行的应用程序的屏幕截图。

inventor.png

关于java - 如何在JavaFX中显示表信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58469842/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com