Scenebuilder中带有分层自定义JavaFx组件的类路径解析 [英] Classpath resolution with hierarchical custom JavaFx components in Scenebuilder

查看:127
本文介绍了Scenebuilder中带有分层自定义JavaFx组件的类路径解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FXML创建自定义组件.自定义组件以分层方式设计.

I'm creating custom components using FXML. The custom components are designed in a hierarchical fashion.

当我设计一个使用另一个自定义组件A的自定义组件B时,场景构建器中会弹出一个类路径问题对话框,我只需通过设置适当的类路径来解决此问题.

When I design a custom component B that uses another custom component A, a classpath problem dialog pops up in scenebuilder and I simply fix this by setting the appropriate classpath.

但是,当我创建三个组件时,例如C包含B包含A,并尝试在Scenebuilder中打开顶级组件C时,它将失败.它要求我提供我适当指定的类路径.它找到B但找不到A.

However when I create three components, say C containing B containing A, and try to open top-level component C in Scenebuilder it fails. It asks me for classpaths which I duly specify. It finds B but does not find A.

由于应用程序能够正确执行,因此类路径,FXML和代码是正确的.只有Scenebuilder出现问题.

The classpath, FXML and the code is correct as the application is able to execute properly. Only Scenebuilder is having problems.

如何使用Scenebuilder打开一个分层的自定义组件?

任何对使用FXML进行层次化组件定义的示例的引用将不胜感激,并可获得50分的奖励. (仅需要3个级别)

Any reference to an example with hierarchical component definitions using FXML would be greatly appreciated and get a bounty of 50 points. (only 3 levels needed)

推荐答案

名叫David的人确实在论坛上回答了您的问题. 出于遗留目的,我将其发布在这里.

Someone named David did answer your question on the forum. For legacy purpose I post it here.

Scene Builder中用于自定义组件的类加载器存在问题. 在SceneBuilder中加载FXML文件时:它使用FXMLLoader及其自己的类加载器. 为了加载使用其自己的FXMLLoader加载其他自定义组件的自定义组件,必须使所有FXMLLoader使用相同的类加载器. 正如David在论坛上所说,您可以通过在自定义组件中添加此代码来实现这一目标.

There is a problem with the classloader in Scene Builder for custom components. When you load a FXML file in SceneBuilder: it uses a FXMLLoader with its own classloader. In order to load custom components which use their own FXMLLoader to load other custom components, it is necessary to make all FXMLLoader use the same classloader. As David said on the forum, you can achieve that by adding this code in your custom component.

public class CustomC extends VBox {
    public CustomC() {
        init();
    }

    private void init() {
        FXMLLoader loader = new FXMLLoader();
        loader.setRoot(this);
        loader.setLocation(this.getClass().getResource("CustomC.fxml"));

        // Make sure to load "CustomC.fxml" with the same classloader that
        // was used to load CustomC class. 
        loader.setClassLoader(this.getClass().getClassLoader());

        try {
           final Node root = (Node)loader.load();
           assert root == this;
        } catch (IOException ex) {
           throw new IllegalStateException(ex);
        }
    }
}

如果要将此代码外部化到一个类中,将此类与您的自定义组件放在同一jar中非常重要:您不能将其放在一个外部jar中(至少现在是这样).

If you want to externalize this code in a class, it is important to put this class in the same jar as your custom components: you cannot put it in a external jar (at least for now).

这篇关于Scenebuilder中带有分层自定义JavaFx组件的类路径解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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