无法使用自定义单元工厂导入自定义组件

编程入门 行业动态 更新时间:2024-10-20 09:34:21
本文介绍了无法使用自定义单元工厂导入自定义组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在JavaFX 2.2中,我一直无法导入在FXML中定义自定义单元工厂的自定义组件。让我们说我的自定义组件是以下

In JavaFX 2.2 I've been having trouble importing custom components that have a custom cell factory defined in the FXML. Lets say my custom component is the following

public class CustomComponent extends VBox{ public CustomComponent() { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomComponent.fxml")); loader.setRoot(this); loader.setController(this); loader.load(); } catch (IOException e ){ throw new RuntimeException(e); } }

}

并且相应的FXML是

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.VBox?> <?import application.*?> <?import application.TestFactory?> <fx:root prefHeight="358.0" prefWidth="260.0" type="VBox" xmlns:fx="javafx/fxml/1" xmlns="javafx/javafx/8"> <children> <TableView prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS"> <columns> <TableColumn prefWidth="75.0" text="C1" > <cellFactory> <TestFactory /> </cellFactory> </TableColumn> <TableColumn prefWidth="75.0" text="C2" > <cellFactory> <TestFactory /> </cellFactory> </TableColumn> </columns> <columnResizePolicy> <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> </columnResizePolicy> </TableView> </children> </fx:root>

和TestFactory定义为

and TestFactory is defined as

public class TestFactory<S,T> implements Callback<TableColumn<S, T>, TableCell<S, T>>{ @Override public TableCell<S, T> call(TableColumn<S, T> param) { return new TableCell<S,T>(); } }

所有这三个文件都是在同一目录/ src包中。

And all three of these files are in the same directory / src package.

当我将这些文件放在一起并尝试将jar导入到scenebuilder中时,它将找不到CustomComponent。但是,如果我拿出对cellFactory& amp;它将导入的TestFactory就好了。看看jar分析它似乎在TestFactory上抛出一个ClassNotFoundException。

When I jar these files together and try to import the jar into scenebuilder it will not find CustomComponent. However, if I take out the reference to the cellFactory & TestFactory it will import just fine. Looking at the jar analysis it seems to throw a ClassNotFoundException on TestFactory.

Not a Node: application/Main.class Not a Node: application/TestFactory.class Exception for: application/CustomComponent.class javafx.fxml.LoadException: unknown path:2 at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425) at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.instantiateWithFXMLLoader(JarExplorer.java:105) at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.exploreEntry(JarExplorer.java:146) at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.explore(JarExplorer.java:65) at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.exploreAndUpdateLibrary(LibraryFolderWatcher.java:298) at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.runDiscovery(LibraryFolderWatcher.java:122) at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.run(LibraryFolderWatcher.java:88) at java.lang.Thread.run(Thread.java:744) Caused by: java.lang.RuntimeException: javafx.fxml.LoadException: file:/C:/Users/bthomas/AppData/Roaming/Scene%20Builder/Library/testing.jar!/application/CustomComponent.fxml at application.CustomComponent.<init>(CustomComponent.java:17) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:408) at java.lang.Class.newInstance(Class.java:433) at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51) at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1010) at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740) at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2723) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ... 8 more Caused by: javafx.fxml.LoadException: file:/C:/Users/bthomas/AppData/Roaming/Scene%20Builder/Library/testing.jar!/application/CustomComponent.fxml at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617) at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2864) at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2708) at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2677) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at application.CustomComponent.<init>(CustomComponent.java:15) ... 18 more Caused by: java.lang.ClassNotFoundException: application.TestFactory at java.URLClassLoader$1.run(URLClassLoader.java:372) at java.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2932) at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2921) at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2862) ... 24 more

我屁股这是场景构建器的一些类路径问题,但我不知所措。有没有人遇到过这个问题或知道解决方案?

I assume this is some classpath issue with scene builder, but i'm at a loss. Has anyone encountered this problem before or know of a solution?

推荐答案

似乎问题出在scenebuilder类加载环境中。这也是scenebuilder的一个已知错误/问题。

It seems the issue lies within the scenebuilder class loading environment. It is also a known bug / issue with scenebuilder.

一种解决方法是将类加载器传播到FXML加载器。

A workaround is to propogate the classloader to the FXML loader.

try { FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomComponent.fxml")); loader.setRoot(this); loader.setController(this); loader.setClassLoader(getClass().getClassLoader()); loader.load(); } catch (IOException e ){ throw new RuntimeException(e); }

更多推荐

无法使用自定义单元工厂导入自定义组件

本文发布于:2023-10-27 03:13:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   组件   单元   工厂

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!