Sto creando un'applicazione JavaFX utilizzando JavaFX Scene Builder. L'interfaccia è stata creata in Scene Builder e un file FXML (main.fxml) è stato creato.Come utilizzare FXMLLoader.load() - JavaFX 2
Per utilizzare l'interfaccia nella mia applicazione, devo caricare il file FXML utilizzando FXMLLoader, ma c'è un problema perché il metodo load() restituisce un oggetto e per costruire una scena ho bisogno di un'istanza della classe Parent.
Di seguito è riportato un pezzo del mio MainClass. Il compilatore sta dando un errore perché la pagina non è di tipo genitore:
Object page = FXMLLoader.load(MainWindowController.class.getResource("main.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.show();
Cosa devo fare per rendere questo compilare ed eseguire correttamente?
Ecco il file FXML (main.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
<children>
<VBox prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<MenuBar minHeight="21.0" prefHeight="21.0" prefWidth="600.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<ToolBar minHeight="24.0">
<items>
<Button fx:id="btFormat" mnemonicParsing="false" text="FORMAT" />
<Button fx:id="btUnformat" mnemonicParsing="false" text="Unformat" />
<Button fx:id="btAutoIndent" mnemonicParsing="false" text="Auto-indent" />
<CheckBox fx:id="cbShowLineNumber" mnemonicParsing="false" text="Show line number" />
</items>
</ToolBar>
<SplitPane dividerPositions="0.5" focusTraversable="true" orientation="VERTICAL" prefHeight="348.0" prefWidth="600.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label text="INPUT - Paste your code here" />
<TextArea fx:id="taInput" prefWidth="200.0" wrapText="true" />
</children>
</VBox>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label text="OUTPUT - The formatted code" />
<TextArea fx:id="taOutput" prefWidth="200.0" wrapText="true" />
</children>
</VBox>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</VBox>
</children>
</AnchorPane>
prega di inserire un 'fonte FXML' nella sua interrogazione. – jewelsea
Aggiunto file FXML. – ceklock