LoadException:根值已在自定义控件上指定 [英] LoadException: Root value already specified on custom control

查看:282
本文介绍了LoadException:根值已在自定义控件上指定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例原因

javafx.fxml.LoadException: Root value already specified.

根据此处的示例编写代码: http://docs.oracle .com/javafx/2/fxml_get_started/custom_control.htm

The code is written according to example here: 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模板之间没有任何联系.

then the view is empty, which is essential, because remains no connection between NavigationView and FXML template.

如何完成?

更新

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="&lt;&lt; Previous" />
      <Button fx:id="nextButton" mnemonicParsing="false" text="Next &gt;&gt;" />
      <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>

推荐答案

使用. <ButtonBar>元素是对FXMLLoader的指令,用于从ButtonBar类创建对象,并且(由于它是FMXL文件的根元素)将其用作创建的结构的根.如果要调用setRoot(this),则已经创建了一个将扮演该角色的对象.

Use a "dynamic root". The <ButtonBar> element is an instruction to the FXMLLoader to create an object from the ButtonBar class, and (since it's the root element of the FMXL file) use it as the root of the structure created. If you want to call setRoot(this), you already have an object created that is going to play that role.

因此,您应该使用:

<?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="&lt;&lt; Previous" />
      <Button fx:id="nextButton" mnemonicParsing="false" text="Next &gt;&gt;" />
      <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>

这篇关于LoadException:根值已在自定义控件上指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆