LoadException:已在自定义控件上指定根值
LoadException: Root value already specified on custom control
下面的例子导致
javafx.fxml.LoadException: Root value already specified.
这里根据例子写代码:http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm
代码:
public class NavigationView extends ButtonBar {
private static final String defaultTemplate = "/fxml/navigator.fxml";
public NavigationView() {
this(null);
}
public NavigationView(URL resource) {
//FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("custom_control.fxml"));
if( resource == null ) {
resource = getClass().getResource(defaultTemplate);
}
FXMLLoader fxmlLoader = new FXMLLoader(resource);
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
public static class Runner extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new NavigationView(), 800, 600);
primaryStage.setTitle("NavigationView");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Runner.launch(args);
}
}
}
如果我删除行
fxmlLoader.setRoot(this);
那么视图是空的,这很重要,因为 NavigationView
和 FXML 模板之间没有任何联系。
如何完成?
更新
FXML
如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<ButtonBar maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="38.0" prefWidth="651.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.91">
<buttons>
<Button fx:id="previousButton" mnemonicParsing="false" text="<< Previous" />
<Button fx:id="nextButton" mnemonicParsing="false" text="Next >>" />
<Button fx:id="editButton" mnemonicParsing="false" text="Edit" />
<Button fx:id="createButton" mnemonicParsing="false" text="Create" />
<Button fx:id="saveButton" mnemonicParsing="false" text="Save" />
<Button fx:id="cancelButton" mnemonicParsing="false" text="Cancel" />
<Button fx:id="deleteButton" mnemonicParsing="false" text="Delete" />
</buttons>
</ButtonBar>
使用 "dynamic root"。 <ButtonBar>
元素是 FXMLLoader
从 ButtonBar
class 创建对象的指令,并且(因为它是 FMXL 文件的根元素)将其用作创建的结构的根。如果您想调用 setRoot(this)
,您已经创建了一个将扮演该角色的对象。
所以你应该使用:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<fx:root type="ButtonBar" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="38.0" prefWidth="651.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.91">
<buttons>
<Button fx:id="previousButton" mnemonicParsing="false" text="<< Previous" />
<Button fx:id="nextButton" mnemonicParsing="false" text="Next >>" />
<Button fx:id="editButton" mnemonicParsing="false" text="Edit" />
<Button fx:id="createButton" mnemonicParsing="false" text="Create" />
<Button fx:id="saveButton" mnemonicParsing="false" text="Save" />
<Button fx:id="cancelButton" mnemonicParsing="false" text="Cancel" />
<Button fx:id="deleteButton" mnemonicParsing="false" text="Delete" />
</buttons>
</fx:root>
下面的例子导致
javafx.fxml.LoadException: Root value already specified.
这里根据例子写代码:http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm
代码:
public class NavigationView extends ButtonBar {
private static final String defaultTemplate = "/fxml/navigator.fxml";
public NavigationView() {
this(null);
}
public NavigationView(URL resource) {
//FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("custom_control.fxml"));
if( resource == null ) {
resource = getClass().getResource(defaultTemplate);
}
FXMLLoader fxmlLoader = new FXMLLoader(resource);
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
public static class Runner extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new NavigationView(), 800, 600);
primaryStage.setTitle("NavigationView");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Runner.launch(args);
}
}
}
如果我删除行
fxmlLoader.setRoot(this);
那么视图是空的,这很重要,因为 NavigationView
和 FXML 模板之间没有任何联系。
如何完成?
更新
FXML
如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<ButtonBar maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="38.0" prefWidth="651.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.91">
<buttons>
<Button fx:id="previousButton" mnemonicParsing="false" text="<< Previous" />
<Button fx:id="nextButton" mnemonicParsing="false" text="Next >>" />
<Button fx:id="editButton" mnemonicParsing="false" text="Edit" />
<Button fx:id="createButton" mnemonicParsing="false" text="Create" />
<Button fx:id="saveButton" mnemonicParsing="false" text="Save" />
<Button fx:id="cancelButton" mnemonicParsing="false" text="Cancel" />
<Button fx:id="deleteButton" mnemonicParsing="false" text="Delete" />
</buttons>
</ButtonBar>
使用 "dynamic root"。 <ButtonBar>
元素是 FXMLLoader
从 ButtonBar
class 创建对象的指令,并且(因为它是 FMXL 文件的根元素)将其用作创建的结构的根。如果您想调用 setRoot(this)
,您已经创建了一个将扮演该角色的对象。
所以你应该使用:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<fx:root type="ButtonBar" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="38.0" prefWidth="651.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.91">
<buttons>
<Button fx:id="previousButton" mnemonicParsing="false" text="<< Previous" />
<Button fx:id="nextButton" mnemonicParsing="false" text="Next >>" />
<Button fx:id="editButton" mnemonicParsing="false" text="Edit" />
<Button fx:id="createButton" mnemonicParsing="false" text="Create" />
<Button fx:id="saveButton" mnemonicParsing="false" text="Save" />
<Button fx:id="cancelButton" mnemonicParsing="false" text="Cancel" />
<Button fx:id="deleteButton" mnemonicParsing="false" text="Delete" />
</buttons>
</fx:root>