我需要在窗格上有一个选择侦听器和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 2.2 中,我在导入具有 FXML 中定义的自定义单元工厂的自定义组件时遇到问题。假设我的自定义组件如下
public class CustomComponent extends VBox{
public CustomComponent() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomComponent.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
} catch (IOException e ){
throw new RuntimeException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
}
而对应的 FXML 是
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<?import application.*?>
<?import application.TestFactory?>
<fx:root prefHeight="358.0" prefWidth="260.0" type="VBox" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<children>
<TableView prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<columns>
<TableColumn prefWidth="75.0" text="C1" >
<cellFactory>
<TestFactory />
</cellFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="C2" >
<cellFactory>
<TestFactory …Run Code Online (Sandbox Code Playgroud)