本文介绍了向 SceneBuilder 2.0 添加自定义组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在窗格上有一个选择侦听器和选择方法,以便能够在单击节点时监视和显示突出显示.

I have the need to have a selection listener and select method on a pane to be able to monitor and present a highlight when a node is clicked on.

我做了以下事情:

public class PaneWithSelectionListener extends Pane {

    private ObjectProperty<Annotation> selectedAnnotation = new SimpleObjectProperty<>();

    public PaneWithSelectionListener() {
        super();
        selectedAnnotation.addListener((obs, oldAnno, newAnno) -> {
            if (oldAnno != null) {
                oldAnno.setStyle("");
            }
            if (newAnno != null) {
                newAnno.setStyle("-fx-border-color: blue;-fx-border-insets: 5;-fx-border-width: 1;-fx-border-style: dashed;");
            }
        });

        setOnMouseClicked(e->selectAnnotation(null));
    }

    public void selectAnnotation(Annotation ann){
        selectedAnnotation.set(ann);
    }
}

这很好用 - 但是我不能再使用 SceneBuilder,因为我的 FXML 引用了这个 PaneWithSelectionListener 而不是 Pane.我不确定如何将我的自定义窗格放入 SceneBuilder.我查看了其他问题,它们都是 FXML 和控制器的组合 - 这只是一个 Pane.

And this works great - however I am not able to work with SceneBuilder anymore since my FXML references this PaneWithSelectionListener rather than Pane. I am not sure how to get my custom pane into SceneBuilder. I have looked at other questions and they are all a combination of FXML and Controllers - where this is just a Pane.

有谁知道这样做的方法,或者在初始化时将 Pane 换成 PaneWithSelectionListener?

Does anyone know of a way to do this, or perhaps swap the Pane for a PaneWithSelectionListener at initialization time?

谢谢

推荐答案

如果问题只是让您的自定义类在 SceneBuilder 中可用,您可以通过以下步骤来实现:

If the issue is just to make your custom class available in SceneBuilder, you can do so with the following steps:

  1. 将您的自定义类(以及任何支持类,例如 Annotation)捆绑为一个 jar 文件
  2. 在 SceneBuilder 中,激活左窗格顶部库"旁边的下拉按钮:
  3. 选择导入 JAR/FXML 文件..."
  4. 选择在步骤 1 中创建的 Jar 文件
  5. 确保您需要在 SceneBuilder (PaneWithSelectionListener) 中访问的类被选中
  6. 按导入组件"
  7. PaneWithSelectionListener 现在将出现在左窗格中自定义"下的 SceneBuilder 中:
  1. Bundle your custom class (and any supporting classes, such as Annotation) as a jar file
  2. In SceneBuilder, activate the drop-down button next to "Library" in the top of the left pane:
  3. Choose "Import JAR/FXML File..."
  4. Select the Jar file created from step 1
  5. Make sure the class you need access to in SceneBuilder (PaneWithSelectionListener) is checked
  6. Press "Import Component"
  7. PaneWithSelectionListener will now appear in SceneBuilder under "Custom" in the left pane:

您会注意到 SceneBuilder 中的下拉菜单有一个自定义库文件夹"选项,您可以从中打开存储 jar 文件的文件夹.对于快速选项,您可以将 jar 文件复制到此文件夹中,并且(经过短暂延迟后),所包含的类将出现在自定义"列表中.

You'll notice the drop-down in SceneBuilder has a "Custom Library Folder" option, from which you can open the folder where the jar files are stored. For a quick option, you can just copy jar files to this folder and (after a short delay), the contained classes will appear in the "Custom" list.

这篇关于向 SceneBuilder 2.0 添加自定义组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 03:08