Dan*_*iel 5 javafx custom-component javafx-2 fxml scenebuilder
我写了一个FXML文件,我在其中使用自定义组件,扩展了Group.加载此自定义组件时,应该将一些子项(此处为:MyOverlayIcon)添加到其子列表中,并按代码设置一些属性(此处为:layoutX和layoutY).
public class MyIcon extends Group {
/********************************
* Graphic
********************************/
private ImageView graphic;
public ImageView getGraphic() {
if(graphic == null) {
graphic = new ImageView();
getChildren().add(graphic);
// Send image to back:
graphic.toBack();
}
return graphic;
}
/********************************
* DataPath
********************************/
private Property<String> dataPath;
public Property<String> dataPathProperty() {
if (dataPath == null) {
dataPath = new SimpleStringProperty();
}
return dataPath;
}
public String getDataPath() {
return dataPathProperty().getValue();
}
public void setDataPath(String dataPath) {
this.dataPathProperty().setValue(dataPath);
getGraphic().setImage(new Image(dataPath));
}
/********************************
* Overlays
********************************/
private ObservableList<MyOverlayIcon> overlays;
public ObservableList<MyOverlayIcon> getOverlays() {
if (overlays == null) {
overlays = FXCollections.observableArrayList();
overlays.addListener(new ListChangeListener<MyOverlayIcon>() {
@Override
public void onChanged(ListChangeListener.Change<? extends MyOverlayIcon> c) {
double moveX = 0.0;
double moveY = 0.0;
double iconWidth = 48.0;
double iconHeight = 48.0;
ObservableList<? extends MyOverlayIcon> icons = c.getList();
MyOverlayIcon icon = icons.get(icons.size() - 1);
String orientation = icon.getOrientation().toString();
if(orientation.endsWith("EAST")) {
moveX = iconWidth / 2;
}
if(orientation.startsWith("SOUTH")) {
moveY = iconHeight / 2;
}
icon.setLayoutX(moveX);
icon.setLayoutY(moveY);
getChildren().add(icon);
}
});
}
return overlays;
}
/********************************
* Constructor
********************************/
public MyIcon(){}
}
Run Code Online (Sandbox Code Playgroud)
public class MyOverlayIcon extends MyIcon {
private EnumOrientation orientation;
public EnumOrientation getOrientation() {
return orientation;
}
public void setOrientation(EnumOrientation orientation) {
this.orientation = orientation;
}
public MyOverlayIcon() {}
}
Run Code Online (Sandbox Code Playgroud)
<Group xmlns:fx="http://javafx.com/fxml">
<MyIcon dataPath="@/images/User1_48.png">
<overlays>
<MyOverlayIcon dataPath="@/images/Remove_24.png" orientation="SOUTHWEST" />
<MyOverlayIcon dataPath="@/images/Search1_24.png" orientation="NORTHEAST" />
</overlays>
</MyIcon>
</Group>
Run Code Online (Sandbox Code Playgroud)
当我在IDE中启动应用程序时,使用FXMLLoader加载此fxml文件,它可以正常工作:所有新子项都添加并正确显示:

现在我想用Scene Builder修改这个fxml文件.但是当我使用Scene Builder加载文件时,我只看到一个空的组实例:

我发现只有这两种解决方法:
1.)调用明确地将子项添加到子列表的方法(例如,在属性的setter方法中),添加并显示子项,但仅在通过修改某些内容而不是在开始时调用此setter时文件已加载.
2.)在调试模式下运行Scene Builder时,我发现,我想要以编程方式修改的任何属性必须先在fxml中指定.否则,Scene Builder会将其值设置为默认值.
例如,我通过java代码动态添加的子项,如果组件未在fxml中指定任何子节点,则再次添加和删除.在fxml中只将任何空节点(如Pane)添加到我的组件中,子节点不会被删除,也会在Scene Builder中显示.
对于其他属性,它只是相同的:应该由代码设置的值(此处:layoutX和layoutY)仅在我使用任何值在fxml文件中设置时才应用.
<Group xmlns:fx="http://javafx.com/fxml">
<MyIcon dataPath="@/images/User1_48.png">
<Pane />
<overlays>
<MyOverlayIcon dataPath="@/images/Remove_24.png" orientation="SOUTHWEST" layoutY="456">
<Pane />
</MyOverlayIcon>
<MyOverlayIcon dataPath="@/images/Search1_24.png" orientation="NORTHEAST" layoutX="123">
<Pane />
</MyOverlayIcon>
</overlays>
</MyIcon>
</Group>
Run Code Online (Sandbox Code Playgroud)

//编辑:如果您看不到链接的图片,请尝试使用此图库链接.
有没有人知道如何使用包含自定义组件的Scene Builder加载fxml文件,该组件通过java代码动态地将子项添加到自身并修改它们(或其)的属性值而不在fxml中指定它们?
| 归档时间: |
|
| 查看次数: |
4558 次 |
| 最近记录: |