В последнее время я работаю над приложением java на основе шаблона MVC. Есть таблица, в которую я вставлю несколько элементов. И когда я использую функцию setItems (), чтобы связать таблицу с моделью. Эта функция не работает. Я уже проверял, верен ли способ привязки файла f xml к соответствующему контроллеру. Нет никаких проблем. Однако, когда я запускаю этот проект и нажимаю кнопку просмотра каталога. Netbeans возвращает эту ошибку «java .lang.reflect.InvocationTargetException». Поэтому мне не удалось вставить элементы в таблицу. Пожалуйста, помогите мне разобраться в том, что происходит. Большое спасибо. С другими файлами проблем нет, потому что я проверял.
Executing F:\UTS\Third Semester\Application programming\Java Slef-study\JavaFx self-StudyDe\48024 SPR 2019 Assignment 2 Skeleton (NETBEANS)\dist\run995722807\48024 SPR 2018 Assignment 2 Skeleton (NETBEANS).jar using platform D:\NetBean\jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: javafx.fxml.LoadException:
file:/F:/UTS/Third%20Semester/Application%20programming/Java%20Slef-study/JavaFx%20self-StudyDe/48024%20SPR%202019%20Assignment%202%20Skeleton%20(NETBEANS)/dist/run995722807/48024%20SPR%202018%20Assignment%202%20Skeleton%20(NETBEANS).jar!/view/catalogue.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at au.edu.uts.ap.javafx.ViewLoader.showStage(ViewLoader.java:20)
at controller.ComputerBuilderController.handleViewCatalogue(ComputerBuilderController.java:23)
... 58 more
Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 62 more
Caused by: java.lang.ClassCastException: model.ComputerBuilder cannot be cast to model.Catalogue
at controller.CatalogueController.getCatalogue(CatalogueController.java:44)
at controller.CatalogueController.initialize(CatalogueController.java:31)
... 72 more
Каталог. java (Модель)
package model;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
public class Catalogue {
private ObservableList<Part> parts;
private ObservableList<Part> currentView;
private final Build build;
public Catalogue(Build build) {
this.build = build;
parts = FXCollections.<Part>observableArrayList();
currentView = FXCollections.<Part>observableArrayList();
parts.add(new Part("evo 860", "storage", 155.00));
parts.add(new Part("daskeyboard", "keyboard", 239.00));
parts.add(new Part("i5", "cpu", 365.00));
parts.add(new Part("corsair 16G", "memory", 299.00));
parts.add(new Part("ASUS ROG", "motherboard", 159.00));
parts.add(new Part("sheetmetal box", "case", 39.00));
parts.add(new Part("Ryzen 7", "cpu", 299.00));
currentView.addAll(parts);
parts.addListener(new ListChangeListener<Part>() {
@Override
public void onChanged(javafx.collections.ListChangeListener.Change<? extends Part> c) {
currentView.clear();
currentView.addAll(parts);
}
});
}
public void addPart(String name, String type, double price) {
parts.add(new Part(name, type, price));
}
public ObservableList<Part> getCurrentView() {
return currentView;
}
public void filterList(String type, String min, String max) {
List<Part> temp = new ArrayList<Part>();
for (Part p : parts) {
try {
if ((type.equals("") || p.hasType(type)) && (min.equals("") || p.costsAtLeast(Double.parseDouble(min)))
&& (max.equals("") || p.costsAtMost(Double.parseDouble(max)))) {
temp.add(p);
}
} catch (NumberFormatException e) {
}
}
this.currentView.clear();
this.currentView.addAll(temp);
}
public void addToBuild(Part p) {
build.addPart(p);
}
public void addToBuild(List<Part> selectedItems) {
build.addParts(selectedItems);
}
public void remove(Part p) {
parts.remove(p);
}
public void remove(List<Part> selectedItems) {
parts.removeAll(selectedItems);
}
}
Каталог.f xml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.*?>
<VBox xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
fx:controller="controller.CatalogueController" alignment="TOP_LEFT" spacing="10" >
<TableView fx:id="CatalogueTb" prefWidth="600" prefHeight="400" >
<placeholder><Label text="No parts in current build." /></placeholder>
<columns>
<TableColumn text="Type" fx:id="typeClm"></TableColumn>
<TableColumn text="Name" fx:id="nameClm"></TableColumn>
<TableColumn text="Price" fx:id="priceClm"></TableColumn>
</columns>
</TableView>
<GridPane alignment="TOP_LEFT" hgap="10" vgap="10">
<Text text="Filters" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2"/>
<Label text="Type:" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField fx:id="typeTf" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Price Range:" GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<TextField fx:id="PriceRangeTf" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
</GridPane>
<Button text="Add Selected to Build" fx:id="AddSelectedBtn"/>
<Button text="Add New Part to Catalogue" fx:id="AddNewBtn"/>
<Button text="Remove Selected from Catalogue" fx:id="RemoveSelectedBtn"/>
<Button text="Close" fx:id="CloseBtn" onAction="#handleClose"/>
</VBox>
Контроллер каталога. java
package controller;
import javafx.collections.*;
import javafx.event.*;
import javafx.fxml.*;
import javafx.scene.text.*;
import javafx.scene.control.*;
import javafx.stage.*;
import javafx.beans.binding.*;
import javafx.beans.property.*;
import java.io.*;
import java.text.*;
import model.*;
import au.edu.uts.ap.javafx.*;
public class CatalogueController extends Controller<Catalogue>{
@FXML private TableView<Part> CatalogueTb;
@FXML private TableColumn<Part, String> typeClm;
@FXML private TableColumn<Part, String> nameClm;
@FXML private TableColumn<Part, String> priceClm;
@FXML private TextField typeTf;
@FXML private TextField PriceRangeTf;
@FXML private Button AddSelectedBtn;
@FXML private Button RemoveSelectedBtn;
@FXML private void initialize() {
CatalogueTb.setItems(model.getCurrentView());
AddSelectedBtn.setDisable(true);
RemoveSelectedBtn.setDisable(true);
typeClm.setCellValueFactory(cellData -> cellData.getValue().typeProperty());
nameClm.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
priceClm.setCellValueFactory(cellData -> cellData.getValue().priceProperty().asString("$%.2f"));
CatalogueTb.getSelectionModel().selectedItemProperty().addListener(
(observable, oldSubject, newSubject) -> AddSelectedBtn.setDisable(newSubject == null));
}