问题描述
我需要在窗格上有一个选择侦听器和select方法,以便在单击节点时能够监视并显示突出显示。
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.
I执行以下操作:
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);
}
}
这很有效 - 但是我无法因为我的FXML引用了这个 PaneWithSelectionListener
而不是 Pane
,所以我不再使用SceneBuilder了。我不确定如何将自定义窗格导入SceneBuilder。我已经查看了其他问题,它们都是FXML和控制器的组合 - 这只是窗格
。
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:
- 捆绑您的自定义类(以及任何支持类,例如
Annotation
)作为jar文件 - 在SceneBuilder中,激活左侧窗格顶部Library旁边的下拉按钮:
- 选择导入JAR / FXML文件...
- 选择从步骤1创建的Jar文件
- 确保您需要在SceneBuilder中访问的类(
PaneWithSelec检查了ingListener
) - 按导入组件
-
PaneWithSelectionListener
现在将出现在左窗格中自定义下的SceneBuilder中:
- Bundle your custom class (and any supporting classes, such as
Annotation
) as a jar file - In SceneBuilder, activate the drop-down button next to "Library" in the top of the left pane:
- Choose "Import JAR/FXML File..."
- Select the Jar file created from step 1
- Make sure the class you need access to in SceneBuilder (
PaneWithSelectionListener
) is checked - Press "Import Component"
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!