我被MVC模式所困

最近,我正在基于MVC模式的Java应用程序上工作。有一张表,我要在其中插入一些元素。当我使用setItems()函数将表链接到模型时。此功能不起作用。我已经检查了将fxml文件链接到相关控制器的方法是否正确。没问题。但是,当我运行该项目并单击视图目录按钮时。 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

Catalogue.java(Model)

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",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);
	}

}

Catalogue.fxml

<?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>

CatalogueController.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));
             
    }

iCMS 回答:我被MVC模式所困

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2109358.html

大家都在问