我需要在窗格上有一个选择侦听器和select方法,以便能够在单击节点时监视并显示突出显示.
我做了以下事情:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这很有效 - 但是我不能再使用SceneBuilder了,因为我的FXML引用了这个PaneWithSelectionListener而不是Pane.我不确定如何将自定义窗格导入SceneBuilder.我已经看了其他问题,它们都是FXML和控制器的组合 - 这只是一个Pane.
有没有人知道这样做的方法,或者可能在初始化时交换Panea PaneWithSelectionListener?
谢谢
我是javaFX的新手.我在java中创建了一个自定义的搜索框(扩展TextField),检查图像:

我用测试类测试了它,它正在工作.
我现在想知道是否可以创建其FXML文件而不是将此组件添加到场景构建器中?怎么做 ?提前致谢.