我在之前的项目中填充了一个TableView,但我从未使用不同的数据类型完成此操作。我试图用我的'Concentrates'表中的数据填充3列,但我不断收到ClassCastException错误,我的getter和setter似乎也没有工作。
我可以使用My controller类中的try while语句在System.print中创建列中的值,但是无法在TableView中获取它们。
我环顾了几个小时,看看文档。我相信这是我的'ConcentrateVolume'列(mlRemaining变量)导致问题,因为它的数据类型是double,但我不确定正确的语法。我原本以为其他2列/变量会在我之前处理过这些数据类型之前完成,并且上一个工作项目的代码大致相同(不是复制/粘贴)。
任何帮助表示赞赏!我正在使用JDK 10.0.1和SQLite数据库。
Inventory.java
package PengVapour;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Inventory extends Application {
public SimpleStringProperty brand = new SimpleStringProperty();
public SimpleStringProperty flavour = new SimpleStringProperty();
public SimpleDoubleProperty mlRemaining = new SimpleDoubleProperty();
public String getBrand() {
return brand.get();
}
public void setBrand(String brandStr) { brand.set(brandStr); }
public SimpleStringProperty brandProperty() {
return brand;
}
public String getFlavour() {
return flavour.get();
}
public void setFlavour(String flavourStr) {
flavour.set(flavourStr);
}
public Double getMlRemaining() {
return mlRemaining.get();
}
public void setMlRemaining(Double mlRemainingStr) { mlRemaining.set(mlRemainingStr); }
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage inventoryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("../FXML/Inventory.fxml"));
VBox root = loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("../CSS/PengVapour.css").toExternalForm());
inventoryStage.setTitle("Peng Vapour - Inventory");
inventoryStage.setScene(scene);
inventoryStage.setResizable(false);
inventoryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
InventoryController.java
package Controllers;
import PengVapour.Inventory;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class InventoryController implements Initializable {
@FXML MenuItem fileExit;
@FXML Button mainMenu;
@FXML TableView table;
@FXML TableColumn columnBrand;
@FXML TableColumn columnFlavour;
@FXML TableColumn columnMlRemaining;
private static Connection con;
private ObservableList<Inventory> invList;
@Override
public void initialize(URL url, ResourceBundle rb) {
invList = FXCollections.observableArrayList();
columnBrand.setCellFactory(
new PropertyValueFactory<Inventory, String>("brand")
);
columnFlavour.setCellFactory(
new PropertyValueFactory<Inventory, String>("flavour")
);
columnMlRemaining.setCellFactory(
new PropertyValueFactory<Inventory, Double>("ml")
);
try {
con = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\PengStation420\\IdeaProjects\\PengVapour\\PVIM.sqlite");
ResultSet rs = con.createStatement().executeQuery("SELECT " +
"Brand, Flavour, ConcentrateVolume " +
"FROM Concentrates");
while (rs.next()) {
Inventory inv = new Inventory();
inv.brand.set(rs.getString("Brand"));
inv.flavour.set(rs.getString("Flavour"));
inv.mlRemaining.set(rs.getDouble("ConcentrateVolume"));
invList.add(inv);
}
table.setItems(invList);
} catch (SQLException ex) {
Logger.getLogger(InventoryController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
public void menuBarFileExit(ActionEvent e) {
System.exit(0);
}
@FXML
public void mainMenuButton(ActionEvent a) throws IOException {
Node node = (Node) a.getSource();
Stage stage = (Stage) node.getScene().getWindow();
SceneController.changeScene(stage, "MainMenu.fxml", "Peng Vapour - Inventory Manager");
}
}
加载Inventory.fxml场景时收到的错误日志:
"C:\Program Files\Java\jdk-10.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.1\lib\idea_rt.jar=58755:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk-10.0.1;C:\Users\PengStation420\IdeaProjects\PengVapour\out\production\PengVapour;C:\Users\PengStation420\Downloads\sqlite-jdbc-3.21.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.1\lib\junit-4.12.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.1\lib\hamcrest-core-1.3.jar" PengVapour.MainMenu
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8865)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3876)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3604)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2613)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
... 47 more
Caused by: java.lang.ClassCastException: javafx.controls@10.0.1/javafx.scene.control.TableColumn cannot be cast to javafx.controls@10.0.1/javafx.scene.control.TableColumn$CellDataFeatures
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:133)
at javafx.controls/javafx.scene.control.skin.TableRowSkin.createCell(TableRowSkin.java:213)
at javafx.controls/javafx.scene.control.skin.TableRowSkin.createCell(TableRowSkin.java:62)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.createCellAndCache(TableRowSkinBase.java:740)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:734)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.<init>(TableRowSkinBase.java:158)
at javafx.controls/javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:89)
at javafx.controls/javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:213)
at javafx.controls/javafx.scene.control.Control.doProcessCSS(Control.java:897)
at javafx.controls/javafx.scene.control.Control.access$000(Control.java:83)
at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:147)
at javafx.graphics/javafx.scene.Node.processCSS(Node.java:9515)
at javafx.graphics/javafx.scene.Node.applyCss(Node.java:9602)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1715)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1692)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1801)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2638)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1211)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1218)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1218)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1218)
at javafx.graphics/javafx.scene.Scene.doLayoutPass(Scene.java:590)
at javafx.graphics/javafx.scene.Scene.preferredSize(Scene.java:1773)
at javafx.graphics/javafx.scene.Scene$2.preferredSize(Scene.java:394)
at javafx.graphics/com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66)
at javafx.graphics/javafx.stage.Window$SceneModel.invalidated(Window.java:826)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.graphics/javafx.stage.Window.setScene(Window.java:782)
at javafx.graphics/javafx.stage.Stage.setScene(Stage.java:270)
at Controllers.SceneController.changeScene(SceneController.java:15)
at Controllers.MainMenuController.viewInventoryButton(MainMenuController.java:54)
... 58 more
Process finished with exit code 0