[英]Custom JavaFX control - “root value already specified” in Scene Builder 2.0
我已经使用fxml文件和Java类实现了一个自定义控件,类似于本官方教程中的解释(参见下面的代码)。 请注意,fxml根元素是使用fx:root
定义的,我setRoot
编程方式调用setRoot
。
我已经尝试将控件包含在应用程序的FXML布局中,并且应用程序加载正常(并按预期显示控件)。
但是,如果我尝试在Scene Builder 2.0中导入包含我的控件的jar文件,则该控件不会出现在要导入的组件列表中(与同一jar中的其他控件不同)。 如果我选择“显示JAR分析报告”,则会显示由javafx.fxml.LoadException: Root value already specified
引起的错误javafx.fxml.LoadException: Root value already specified
。
你知道为什么我在Scene Builder中加载时会出现这个错误,即使它在实际应用程序中正确加载了吗?
这是FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<fx:root type="javafx.scene.layout.GridPane" id="MediaMetadataDisplay" hgap="20.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints fillWidth="false" hgrow="NEVER" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="200.0"/>
<ColumnConstraints halignment="LEFT" hgrow="ALWAYS"/>
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="40.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="25.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="25.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="25.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="25.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="30.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<ImageView id="coverView" fx:id="coverView" fitHeight="200.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.rowSpan="7"/>
<Label id="trackName" fx:id="trackName" maxWidth="1.7976931348623157E308" text="trackName" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font name="System Bold" size="16.0"/>
</font>
</Label>
<Label id="artist" fx:id="artist" maxWidth="1.7976931348623157E308" text="artist" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Label id="album" fx:id="album" maxWidth="1.7976931348623157E308" text="album" GridPane.columnIndex="1" GridPane.rowIndex="3"/>
<Label id="genre" fx:id="genre" maxWidth="1.7976931348623157E308" text="genre" GridPane.columnIndex="1" GridPane.rowIndex="4"/>
<Label id="trackNumber" fx:id="trackNumber" maxWidth="1.7976931348623157E308" text="trackNumber" GridPane.columnIndex="1" GridPane.rowIndex="5"/>
</children>
</fx:root>
和Java控制器/根元素:
package customjavafx.scene.control;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.media.Media;
import java.io.IOException;
import java.util.Map;
public class MediaMetadataDisplay extends GridPane {
public MediaMetadataDisplay() {
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MediaMetadataDisplay.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
media.addListener((obs, oldVal, newVal) -> updateMedia(newVal));
}
private final ObjectProperty<Media> media = new SimpleObjectProperty<>((Media) null, "media");
@FXML private ImageView coverView;
@FXML private Label trackName;
@FXML private Label artist;
@FXML private Label album;
@FXML private Label genre;
@FXML private Label trackNumber;
public void updateMedia(Media media) {
// TODO show updated metadata
}
public ObjectProperty<Media> mediaProperty() {
return media;
}
public Media getMedia() {
return mediaProperty().get();
}
public void setMedia(final Media media) {
mediaProperty().set(media);
}
}
在Scene Builder显示的堆栈跟踪中出错的原因:
Caused by: javafx.fxml.LoadException: Root value already specified.
file:/Users/guillaumegaly/Library/Application%20Support/Scene%20Builder/Library/custom-controls_2.11.jar!/customjavafx/scene/control/MediaMetadataDisplay.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2613)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2771)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2720)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at customjavafx.scene.control.MediaMetadataDisplay.<init>(MediaMetadataDisplay.java:26)
... 18 more
删除该行
fxmlLoader.setRoot(this);
你的FXML将root定义为AnchorPane(你不能设置root两次,这就是你得到错误的原因)。
<fx:root type="javafx.scene.layout.GridPane" id="MediaMetadataDisplay" hgap="20.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
我非常感谢这篇文章
我正在关注文档中的Javafx FXML教程,并且我陷入了自定义组件部分。 对我来说问题是,当我调用setRoot方法时,它会抛出“Root already specified”错误。 就个人而言,我通过将根标记添加到自定义组件FXML文档中来修复此问题。 它现在有效。
我添加这个,以便如果其他人有同样的问题,谷歌可以给他们答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.