2016-05-02 4 views
1

例は、以下のコードは、ここでは例に従って書かれLoadException:既にカスタムコントロールに指定されたルート値は

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がある

UPDATEは、次のとおりです。

<?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> 

答えて

2

"dynamic root"を使用してください。 <ButtonBar>要素は、ButtonBarクラスからオブジェクトを作成するための指示であり(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="&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> 
+0

しかし、私が使用したい場合は、 '' を?私はFXMLの外に余分なコンテナを作成することで回避策を見つけました。他の方法はありますか? – Dims

+0

これは、ボタンバーを作成します。これは、ルートとして設定しているオブジェクトがButtonBarインスタンスであるためです。NavigationViewは、ButtonBarを拡張しています。 –

+0

私は '' *と 'ButtonBar'のサブクラスを作成すると、2つのボタンバーがあります。なぜ彼らの2つが欲しいのですか? –

関連する問題