简体   繁体   English

如何设置要导入到Scene Builder中的JavaFX控件?

[英]How do I set up a JavaFX Control to be imported into Scene Builder?

I have a JavaFX control that is basically an amalgamation of several other JavaFX controls. 我有一个JavaFX控件,基本上是其他几个JavaFX控件的合并。

I want it such that the .jar file can be imported into Scene Builder so that it can be used like any other control. 我希望它可以将.jar文件导入Scene Builder,以便可以像其他任何控件一样使用它。 The closest analogy I can think of is when you make a custom control in C# and use it several times throughout several projects. 我能想到的最接近的类比是当您在C#中创建自定义控件并在多个项目中多次使用它时。

When I try to import the FXML file, it doesn't work. 当我尝试导入FXML文件时,它不起作用。 The control isn't treated as a single entity, and instead is basically just all of it's parts strung out in the FXML file. 控件不被视为单个实体,而是基本上只是FXML文件中的所有部分。

What do I need to do with the FXML file, or the controller.java file so that the Scene Builder will be able to import the .jar, see the control(s), and allow me to import and use each custom control as a single entity? 我需要对FXML文件或controller.java文件执行什么操作,以便Scene Builder能够导入.jar,查看控件,并允许我将每个自定义控件导入并使用为单一实体? I've looked several places and even asked on Stack Overflow once before (though the answer I got was not the one for which I was looking, and have received no responses since), but nothing I've seen comes close to handling my issue. 我看过几个地方,甚至以前曾在Stack Overflow上问过一次(尽管我得到的答案不是我一直在寻找的答案,并且此后未收到任何答复),但是我所看到的一切都无法解决我的问题。

The closest I've come has to do with this line in the FXML file: 我最接近的与FXML文件中的这一行有关:

<?scenebuilder-classpath-element /path/to/something?>

but I don't know what goes in /path/to/something 但我不知道/path/to/something

I know I can, in the initialization, simply add the control to the scene, but that is sub-optimal and something which I am desperately trying to avoid. 我知道我可以在初始化中简单地将控件添加到场景中,但这是次优的,我非常想避免。

I was finally able to resolve the issue. 我终于能够解决问题。 After much trial and error and following the sample code that came from here , I discovered my problem was that I needed 2 classes for each FXML control group. 经过大量的试验和错误并遵循此处的示例代码,我发现我的问题是每个FXML控件组需要2个类。 One to be the actual controller of the group, and another to be the object that would house the controller. 一个是该组的实际控制者,另一个是将容纳该控制器的对象。 I followed the code in the Unlock example and it was a godsend for helping me. 我遵循了Unlock示例中的代码,这对帮助我真是天赐良机。 Basically it comes down to two files: 基本上,它分为两个文件:

The object (which extends the type of the root node, in my case): 对象(在我的例子中,它扩展了根节点的类型):

public class <InsertObjectClassNameHere> extends <RootContainerTypeHere>{
}

After that you need the controller class. 之后,您需要控制器类。 This is with what I am most familiar, however I was still doing it wrong. 这是我最熟悉的,但是我仍然做错了。 This is what needs to implement initializable : 这是implement initializable需要:

public class <InsertControllerClassNameHere> implements Initializable{

}

So for me the Object class looks like this: 所以对我来说,Object类是这样的:

public class DGCSDefiner extends GridPane {
    private final DGCSDefinerController Controller;
    public DGCSDefiner(){
        this.Controller = this.Load();
    }

    private DGCSDefinerController Load(){
        final FXMLLoader loader = new FXMLLoader();
        loader.setRoot(this);
        loader.setClassLoader(this.getClass().getClassLoader());
        loader.setLocation(this.getClass().getResource("DGCSDefiner.fxml"));

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

        final DGCSDefinerController cntrlr = loader.getController();
        assert cntrlr != null;
        return cntrlr;
    }

    /**
     * Get the settings defined by the controller.
     * @return controller defined settings.
     */
    public ColorSettings getColorSettings(){
        return this.Controller.getColorSettings();
    }

    /**
     * Set the controllers color settings.
     * @param CS Defined color settings.
     */
    public void setColorSettings(ColorSettings CS){
        this.Controller.setColorSettings(CS);
    }
}

and then there is the actual Controller class. 然后是实际的Controller类。

So for a straight-forward answer, 因此,对于一个简单的答案,

you need to have a class that will be loading your controller, and you need to pass down from your controller to that class that with which you will be working (Or, you can simply keep the controller public and access it directly). 您需要有一个将加载控制器的类,并且需要从控制器传递到将要使用的类(或者,您可以简单地将控制器公开并直接访问它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM