无法使用自定义单元工厂导入自定义组件 [英] Can't import custom components with custom cell factories

查看:163
本文介绍了无法使用自定义单元工厂导入自定义组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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="http://javafx.com/fxml/1" xmlns="http://javafx.com/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.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.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);
}

这篇关于无法使用自定义单元工厂导入自定义组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆