FXML代码 -
<TreeItem expanded="true" value="1">
<graphic>
<ImageView fx:id="folderImageView"/>
</graphic>
<children>
<TreeItem value="11" />
</children>
</TreeItem>
<TreeItem expanded="true" value="2">
<graphic>
<ImageView fx:id="folderImageView"/>
</graphic>
<children>
<TreeItem value="2" />
</children>
</TreeItem>
这是控制器代码 -
public class LController implements Initializable {
@FXML ImageView folderImageView;
public void initialize(URL location, ResourceBundle resources) {
folderImageView.setImage(new Image("/resource/b.png"));
}
图像b.png仅在最后一个TreeItem中显示(即,其值为2)
你能建议吗?
答案 0 :(得分:0)
每个TreeItem
应该有自己的ImageView
。因此,解决方案是为FXML中的每个ImageView
定义唯一的 fx:id 。或者为TreeView
提供一个fx:id =“myTreeView”,然后在控制器中初始化两个treeitems及其图形。
答案 1 :(得分:0)
关于Jira的更详细的解决方案 - https://javafx-jira.kenai.com/browse/RT-33190
基本上在 -
<AnchorPane xmlns:fx="http://javafx.com/fxml">
<children>
<TreeView id="tView">
<root>
<include>
<Image fx:id= "img" url="@b.png" />
</include>
<TreeItem expanded="true" value="Components">
<graphic>
<ImageView>
<image>
<fx:reference source = "img"/>
</image>
</ImageView>
</graphic>
<children>
<TreeItem value="User" />
</children>
</TreeItem>
</root>
</TreeView>
</children>
</AnchorPane>